JPA使用bug備忘錄

1. Datejava

@Entity
@Table(name = "abc")
public class Abc{
    //ID
    @Id
    @Column(name = "abc_id")
    private String abcId;
    //內容(文本)
    @Column(name = "abc_text",length = 21700)
    private String abcText;
    //建立人
    @Column(name = "user_id")
    private long userId;
    //建立時間
    @Column(name = "create_time")
    private Date createTime;}

1)Date類型 在轉化成mysql的DateTime時會丟失時間.mysql

2)Date在轉mysql的 timeStamp類型時5.3.0版本會存在Bug, 一直到5.3.5才修復sql

3)mysql默認設置 CURRENT_TIMESTAMP,在沒有值時會使用數據庫時間;數據庫

CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP

在修改記錄時 時間自動更新。this

2. mysql varchar二、text在存中文時的字符長度含義有區別spa

三、實體定義Quality參數校驗
@Data
@Document(collection = "Quality")
public class Quality{
    private String id;
    @Size(min = 1, max = 100)
    @NotNull
    private String userId;
    private Date createTime;
    @Min(value = 0)
    private Integer score;    
}.net

四、Repository定義
@Repository(value = "qualityRepository")
public interface QualityRepository extends MongoRepository<Quality, String> {
    List<Quality> findByIdIn(List<String> ids);
    List<Quality> findByScoreGreaterThan(int score);
    Quality findTopByOrderByScoreDesc();
    Page<Quality> findByUserId(String userId, Pageable page);排序

List<Quality> findByCreateTimeBetween(Date startTime, Date endTime);  //日期參數運行環境java8,其它版本沒有用過文檔

}get

五、排序查詢示例
 Sort sort = new Sort(Sort.Direction.DESC,"createTime"); //建立時間降序排序
 Pageable pageable = new PageRequest(pageNumber,pageSize,sort);
 this.qualityRepository.findByUserId(userId,pageable);
 
六、其它,關聯查詢
1.新建個類 ABResult ,類全名 com.abc.ABResult ,寫好構造方法 public ABResult(String aName, String aCd, String bName)
2.JPQL:"SELECT new com.abd.xxResult(a.name, a.cd, b.name) FROM TableA a LEFT JOIN FETCH a.b"
3.你的 T 就是 ABResult
這種 new 的寫法咱們一直在用,不知道合不合你的需求。

參考文檔 https://www.jianshu.com/p/0ad1c060c46b findby

相關文章
相關標籤/搜索