在我的客户端服务器应用程序中,我使用javafx作为客户端,Java Hibernate作为服务器连接。
问题
我有一个保存按钮,对于每次单击按钮,Envers会创建一个修订版,是否在表值没有更改。基本上,它没有将旧数据检查到新数据,而只是创建修订。
这是我的豆代码
@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 DATA
{ " domain_id":36, " domain_name":" test_type", " domain_data_type":"字符串", " domain_eff_start_date":" 2016-11-08", " domain_eff_end_date":" 2099-12-31", " domain_status":"草稿", " domain_description":"对象类型:添加以进行测试目的"}
对不起,我没有很快看到这个,但是问题是您对session#save
的呼吁。在此处的Session
的Javadocs中,您会注意到以下段落:
save()和persist()在sql delete和update()或sql update中的sql delete()中导致sql insert,delete()。在冲洗时间检测到持续实例的更改,也导致SQL更新。saveorupdate()和replicate()在插入或更新中结果。
因此,您基本上想使用session#saveOrUpdate
,以便根据实体状态获得插入和更新语义。
由于session#save
正在生成新的INSERT
操作,因此Envers将始终为该情况创建新的修订。