数据库表的自增ID createDate和updateDate 用JPA注解代替触发器实现

对于数据库表的自增ID , createDate和updateDate 等字段,用JPA注解代替触发器实现,效率会高很多。 
由于这些属性很多entity都有 可以写成两个基本entity :BaseEntity和AbstractEntity 然后其他entity继承BaseEntity即可

BaseEntity

@MappedSuperclass
public class BaseEntity extends AbstractEntity {
    @Id
    @Column(
        name = "ID"
    )
    @GeneratedValue(
        strategy = GenerationType.AUTO
    )
    private Long id;

    public BaseEntity() {
    }

    public Long getId() {
        return this.id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public boolean equals(Object o) {
        if(o == this) {
            return true;
        } else if(!(o instanceof BaseEntity)) {
            return false;
        } else {
            BaseEntity other = (BaseEntity)o;
            if(!other.canEqual(this)) {
                return false;
            } else {
                Long this$id = this.getId();
                Long other$id = other.getId();
                if(this$id == null) {
                    if(other$id != null) {
                        return false;
                    }
                } else if(!this$id.equals(other$id)) {
                    return false;
                }

                return true;
            }
        }
    }

    protected boolean canEqual(Object other) {
        return other instanceof BaseEntity;
    }

    public int hashCode() {
        boolean PRIME = true;
        byte result = 1;
        Long $id = this.getId();
        int result1 = result * 59 + ($id == null?0:$id.hashCode());
        return result1;
    }

    public String toString() {
        return "BaseEntity(id=" + this.getId() + ")";
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64

AbstractEntity

@MappedSuperclass
public abstract class AbstractEntity implements Serializable {
    @Column(
        name = "CREATE_DATE",
        nullable = false,
        updatable = false
    )
    private Date createDate;
    @Column(
        name = "UPDATE_DATE",
        nullable = false
    )
    private Date updateDate;

    protected void touchCreateTime() {
        this.createDate = new Date();
    }

    protected void touchUpdateTime() {
        this.updateDate = new Date();
    }

    @PrePersist
    public void fireCreated() {
        this.touchCreateTime();
        this.touchUpdateTime();
    }

    @PreUpdate
    public void fireUpdated() {
        this.touchUpdateTime();
    }

    public AbstractEntity() {
    }

    public Date getCreateDate() {
        return this.createDate;
    }

    public Date getUpdateDate() {
        return this.updateDate;
    }

    public void setCreateDate(Date createDate) {
        this.createDate = createDate;
    }

    public void setUpdateDate(Date updateDate) {
        this.updateDate = updateDate;
    }

    public boolean equals(Object o) {
        if(o == this) {
            return true;
        } else if(!(o instanceof AbstractEntity)) {
            return false;
        } else {
            AbstractEntity other = (AbstractEntity)o;
            if(!other.canEqual(this)) {
                return false;
            } else {
                Date this$createDate = this.getCreateDate();
                Date other$createDate = other.getCreateDate();
                if(this$createDate == null) {
                    if(other$createDate != null) {
                        return false;
                    }
                } else if(!this$createDate.equals(other$createDate)) {
                    return false;
                }

                Date this$updateDate = this.getUpdateDate();
                Date other$updateDate = other.getUpdateDate();
                if(this$updateDate == null) {
                    if(other$updateDate != null) {
                        return false;
                    }
                } else if(!this$updateDate.equals(other$updateDate)) {
                    return false;
                }

                return true;
            }
        }
    }

    protected boolean canEqual(Object other) {
        return other instanceof AbstractEntity;
    }

    public int hashCode() {
        boolean PRIME = true;
        byte result = 1;
        Date $createDate = this.getCreateDate();
        int result1 = result * 59 + ($createDate == null?0:$createDate.hashCode());
        Date $updateDate = this.getUpdateDate();
        result1 = result1 * 59 + ($updateDate == null?0:$updateDate.hashCode());
        return result1;
    }

    public String toString() {
        return "AbstractEntity(createDate=" + this.getCreateDate() + ", updateDate=" + this.getUpdateDate() + ")";
    }
}
转自CSDN https://blog.csdn.net/hikeboy/article/details/56006987
原文地址:https://www.cnblogs.com/dauber/p/9115175.html