JPA @CreatedBy@CreatedDate@LastModifiedBy@LastModifiedDate

错误更正

上一篇博客中写到重写JPA的源码,经过考虑发现没有必要那么麻烦,只要annotation正确,实体命名和数据库对应是可以不同的

新的做法

    @CreatedBy
    @Column(name = "create_id", nullable = false, length = 50)
    protected String createId;

    @CreatedDate
    @Column(name = "create_time", nullable = false)
    protected Date createTime;

    @LastModifiedBy
    @Column(name = "update_id", length = 50)
    protected String updateId;


    @LastModifiedDate
    @Column(name = "update_time")
    protected Date updateTime;

特别注意

    /*** 默认0,必填,离线乐观锁 */
    @Version
    @Column(name = "version_")
    protected Integer version = 0;

乐观锁所import的version是

    import javax.persistence.Version

而不是JPA中的

    import org.springframework.data.annotation.Version

使用后者虽然不会报错,但是乐观锁不起作用。
而且version = 0,不然多对多或者多对一级联更新的时候会因为页面传上来的对象中version=null导致InvalidDataAccessApiUsageException

org.springframework.dao.InvalidDataAccessApiUsageException: 
org.hibernate.TransientObjectException: object references an unsaved transient instance 
- save the transient instance before flushing: 

猜你喜欢

转载自blog.csdn.net/op_kapu/article/details/77840942
JPA