1. 程式人生 > >資料庫表的自增ID createDate和updateDate 用JPA註解代替觸發器實現

資料庫表的自增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() + ")"; } }

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() + ")";
    }
}