您的位置:首页 > 数据库

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

2017-02-20 09:53 417 查看
对于数据库表的自增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() + ")";
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: