在我的客户端服务器应用程序中,我使用JavaFx作为客户端,JavaHibernate作为服务器到数据库的连接。
问题
我有一个保存按钮,每次点击按钮,envers都会创建一个修订版,表值是否没有变化。基本上它不是将旧数据检查为新数据,只是创建修订版。
这是我的bean代码
@Entity
@Audited
@Table(name = "DOMAIN")
public class Domain implements java.io.Serializable {
private Long domainId;
private String domainName;
private String dataType;
private Long valueSize;
private Long valuePrecision;
private Long valueScale;
private String valueRangeLow;
private String valueRangeHigh;
private String description;
private String comments;
private Date effectiveStartDate;
private Date effectiveEndDate;
private String status;
public Domain() {
}
public Domain(Long domainId, String domainName, String dataType,
Date effectiveStartDate, Date effectiveEndDate, String status) {
this.domainId = domainId;
this.domainName = domainName;
this.dataType = dataType;
this.effectiveStartDate = effectiveStartDate;
this.effectiveEndDate = effectiveEndDate;
this.status = status;
}
public Domain(Long domainId, String domainName, String dataType,
Long valueSize, Long valuePrecision,
Long valueScale, String valueRangeLow, String valueRangeHigh,
String description, String comments, Date effectiveStartDate,
Date effectiveEndDate, String status) {
this.domainId = domainId;
this.domainName = domainName;
this.dataType = dataType;
this.valueSize = valueSize;
this.valuePrecision = valuePrecision;
this.valueScale = valueScale;
this.valueRangeLow = valueRangeLow;
this.valueRangeHigh = valueRangeHigh;
this.description = description;
this.comments = comments;
this.effectiveStartDate = effectiveStartDate;
this.effectiveEndDate = effectiveEndDate;
this.status = status;
}
@Id
@GeneratedValue(generator = "DOMAIN_SEQ")
@SequenceGenerator(name="DOMAIN_SEQ", sequenceName="DOMAIN_SEQ",allocationSize=1)
@Column(name = "DOMAIN_ID", unique = true, nullable = false, precision = 22, scale = 0)
public Long getDomainId() {
return this.domainId;
}
public void setDomainId(Long domainId) {
this.domainId = domainId;
}
@Column(name = "DOMAIN_NAME", nullable = false, length = 50)
public String getDomainName() {
return this.domainName;
}
public void setDomainName(String domainName) {
this.domainName = domainName;
}
@Column(name = "DATA_TYPE", nullable = false, length = 50)
public String getDataType() {
return this.dataType;
}
public void setDataType(String dataType) {
this.dataType = dataType;
}
@Column(name = "VALUE_SIZE", precision = 22, scale = 0)
public Long getValueSize() {
return this.valueSize;
}
public void setValueSize(Long valueSize) {
this.valueSize = valueSize;
}
@Column(name = "VALUE_PRECISION", precision = 22, scale = 0)
public Long getValuePrecision() {
return this.valuePrecision;
}
public void setValuePrecision(Long valuePrecision) {
this.valuePrecision = valuePrecision;
}
@Column(name = "VALUE_SCALE", precision = 22, scale = 0)
public Long getValueScale() {
return this.valueScale;
}
public void setValueScale(Long valueScale) {
this.valueScale = valueScale;
}
@Column(name = "VALUE_RANGE_LOW", length = 50)
public String getValueRangeLow() {
return this.valueRangeLow;
}
public void setValueRangeLow(String valueRangeLow) {
this.valueRangeLow = valueRangeLow;
}
@Column(name = "VALUE_RANGE_HIGH", length = 50)
public String getValueRangeHigh() {
return this.valueRangeHigh;
}
public void setValueRangeHigh(String valueRangeHigh) {
this.valueRangeHigh = valueRangeHigh;
}
@Column(name = "DESCRIPTION", length = 200)
public String getDescription() {
return this.description;
}
public void setDescription(String description) {
this.description = description;
}
@Column(name = "COMMENTS")
public String getComments() {
return this.comments;
}
public void setComments(String comments) {
this.comments = comments;
}
@Temporal(TemporalType.DATE)
@Column(name = "EFFECTIVE_START_DATE", nullable = false, length = 7)
public Date getEffectiveStartDate() {
return this.effectiveStartDate;
}
public void setEffectiveStartDate(Date effectiveStartDate) {
this.effectiveStartDate = effectiveStartDate;
}
@Temporal(TemporalType.DATE)
@Column(name = "EFFECTIVE_END_DATE", nullable = false, length = 7)
public Date getEffectiveEndDate() {
return this.effectiveEndDate;
}
public void setEffectiveEndDate(Date effectiveEndDate) {
this.effectiveEndDate = effectiveEndDate;
}
@Column(name = "STATUS", nullable = false, length = 50)
public String getStatus() {
return this.status;
}
public void setStatus(String status) {
this.status = status;
}
问题仅在于客户端服务器应用程序。
使用正常的独立程序,它可以正常工作。
有人能帮我吗?我在这一点上卡住了。我缺少任何罐子或其他东西吗?如果你需要更多关于问题的澄清,请告诉我。
服务器端代码
public long saveDomainFromJson(String domainJsonData) throws Exception {
long domainId = 0;
try {
// here I am setting data to bean, getting from client side
Domain domain = getDomainFromJson(domainJsonData);
Session session = HibernateUtil.currentSession();
Transaction tx = session.beginTransaction();
domainId = session.saveOrUpdate(domain);
tx.commit();
HibernateUtil.closeSession();
} catch (Exception e) {
e.printStackTrace();
throw e;
}
return domainId;
}
Json数据
{ "DOMAIN_ID":36, "DOMAIN_NAME":"Test_Type", "DOMAIN_DATA_TYPE":"STRING", "DOMAIN_EFF_START_DATE":"2016-11-08", "DOMAIN_EFF_END_DATE":"2099-12-31", "DOMAIN_STATUS":"Draft", "DOMAIN_DESCRIPTION":"Object Type: Added for testing purpose" }
抱歉,我没有很快看到这一点,但问题是您对session#sav
的调用。从这里的Session
的javadocs中,您会注意到以下段落:
保存()和持久化()会导致SQLINSERT,删除()SQLDELETE和update()或merge()SQLUPDATE。对持久化实例的更改会在刷新时检测到,并会导致SQLUPDATE。saveOrUpdate()和复制()会导致INSERT或UPDATE。
因此,您基本上想要使用session#saveOrUpdate
,以便根据实体的状态获取插入和更新语义学。
由于session#sav
正在生成一个新的INSERT
操作,Envers将始终为该情况创建一个新的修订版。