数据库表的自增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() + ")";
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值