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

相关文章
相关标签/搜索