数据库表的自增ID createDate和updateDate 用JPA注解代替触发器实现
2017-02-20 09:53
417 查看
对于数据库表的自增ID , createDate和updateDate 等字段,用JPA注解代替触发器实现,效率会高很多。
由于这些属性很多entity都有 可以写成两个基本entity :BaseEntity和AbstractEntity 然后其他entity继承BaseEntity即可
BaseEntity
AbstractEntity
由于这些属性很多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() + ")"; } }
相关文章推荐
- 使用JPA中@Query 注解实现update 操作
- 使用JPA中@Query 注解实现update 操作
- Jpa配置实体类创建时间更新时间自动赋值,@CreateDate,@LastModifiedDate
- 使用JPA中@Query 注解实现update 操作方法(必看)
- Jpa配置实体类创建时间更新时间自动赋值,@CreateDate,@LastModifiedDate
- 使用JPA中@Query 注解实现update 操作
- 使用JPA中@Query 注解实现update 操作
- 开发记录——触发器实现 数据库关联字段
- 【原创】GSM手机轻松实现定位 (由基站地址码及ID查询数据库)
- 通过触发器实现数据库的即时同步
- MySQL存储过程和触发器的实现--数据库学习笔记
- 通过触发器实现数据库的即时同步
- 通过触发器实现数据库的即时同步
- 通过触发器实现数据库的即时同步
- 如何通过触发器实现数据库的即时同步
- 如何通过触发器实现数据库的即时同步
- 关于触发器实现跨数据库的操作问题
- [数据库]一个利用触发器(trigger)实现数据库表的审计功能(audit)的例子--针对ms sql实现
- 通过触发器实现数据库的即时同步
- 通过触发器实现数据库的即时同步