我是JPA的新手,我正在努力坚持一个将持续3个阶段New,Processing和Completed的实体。我希望以下时间戳相应更新
@Entity
@Table(name = "feedertable")
@JsonIgnoreProperties(value = { "processStatus" })
public class FeederTable {
@Id
@GeneratedValue(strategy= GenerationType.AUTO)
private String id;
private String orderID;
private String status;
@Column(name="CREATEDDATETIME",nullable = false)
private Timestamp createdDateTime;
@Column(name="PROCESSSTATUS",nullable = false)
private String processStatus;
@Column(name="HOSTNAME",nullable = true)
private String hostName;
//@Temporal(TemporalType.TIMESTAMP)
@Column(name="PROCESSINGDATETIME",nullable = false)
private Timestamp processingDateTime;
@Column(name="COMPLETEDDATETIME",nullable = false)
private Timestamp completedDateTime; public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getOrderID() {
return orderID;
}
public void setOrderID(String orderID) {
this.orderID = orderID;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public Timestamp getCreatedDateTime() {
return createdDateTime;
}
public void setCreatedDateTime(Timestamp createdDateTime) {
this.createdDateTime = createdDateTime;
}
public String getProcessStatus() {
return processStatus;
}
public void setProcessStatus(String processStatus) {
this.processStatus = processStatus;
}
public String getHostName() {
return hostName;
}
public void setHostName(String hostName) {
this.hostName = hostName;
}
public Timestamp getProcessingDateTime() {
return processingDateTime;
}
public void setProcessingDateTime(Timestamp processingDateTime) {
this.processingDateTime = processingDateTime;
}
public Timestamp getCompletedDateTime() {
return completedDateTime;
}
public void setCompletedDateTime(Timestamp completedDateTime) {
this.completedDateTime = completedDateTime;
}
@Override
public String toString() {
return "FeederTable{" +
"id='" + id + '\'' +
", orderID='" + orderID + '\'' +
", status='" + status + '\'' +
", createdDateTime=" + createdDateTime +
", processStatus='" + processStatus + '\'' +
", hostName='" + hostName + '\'' +
", processingDateTime=" + processingDateTime +
", completedDateTime=" + completedDateTime +
'}';
}
坚持使用JPARepository:
FeederTable feederTable = new FeederTable();
feederTable.setOrderID(orderId);
feederTable.setStatus(status);
feederTable.setCreatedDateTime(new Timestamp(format.getTime())); feederTable.setProcessStatus("New");
feederTable.setHostName(hostname);
feederTable.setCompletedDateTime(null);
feederTable.setProcessingDateTime(null);
repository.save(feederTable);
但是,当第一次保留实体时,将填充所有时间戳。我希望PROCESSINGDATETIME和COMPLETEDDATETIME为null并仅在它达到相应状态时填充它。知道如何实现它吗?。
答案 0 :(得分:0)
我使用@PrePersist
和@PreUpdate
注释在我的项目中做了类似的事情。
您可以使用其他生命周期钩子。阅读他们here。
MyEntity{
@Temporal(TemporalType.TIMESTAMP)
private Date creationDate;
@Temporal(TemporalType.TIMESTAMP)
private Date updationDate;
@PrePersist
public void onCreate() {
creationDate = new Date();
}
@PreUpdate
public void onUpdate() {
updationDate = new Date();
}
}
我是否正确理解了您的问题?这是你在找什么?