Java 对于每个保存(更新)的hibernate envers,创建的修订在表中更改或未更改
在我的客户机-服务器应用程序中,我使用JavaFx作为客户机,使用javahibernate作为服务器到数据库的连接 问题 我有一个保存按钮,每次单击按钮,envers都会创建一个修订,不管表值是否有变化。 基本上,它不是将旧数据检查为新数据,而是创建修订 这是我的bean代码Java 对于每个保存(更新)的hibernate envers,创建的修订在表中更改或未更改,java,hibernate,hibernate-envers,Java,Hibernate,Hibernate Envers,在我的客户机-服务器应用程序中,我使用JavaFx作为客户机,使用javahibernate作为服务器到数据库的连接 问题 我有一个保存按钮,每次单击按钮,envers都会创建一个修订,不管表值是否有变化。 基本上,它不是将旧数据检查为新数据,而是创建修订 这是我的bean代码 @Entity @Audited @Table(name = "DOMAIN") public class Domain implements java.io.Serializable { private Long d
@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"
}
{
“域ID”:36,
“域名”:“测试类型”,
“域数据类型”:“字符串”,
“域名生效日期”:“2016-11-08”,
“域名生效日期”:“2099-12-31”,
“域_状态”:“草稿”,
“域描述”:“对象类型:添加用于测试目的”
}
很抱歉,我没有很快看到此消息,但问题是您呼叫
会话#save
。在会话的javadocs中,您会注意到以下段落:
save()和persist()会导致SQL插入、SQL删除和更新()中的删除()或SQL更新中的合并()。在刷新时检测到对持久实例的更改,并导致SQL更新。saveOrUpdate()和replicate()会导致插入或更新
因此,您基本上希望使用session#saveOrUpdate
,以便根据实体的状态获得插入和更新语义
由于session#save
正在生成一个新的INSERT
操作,因此Envers将始终为该情况创建一个新的修订版。是否每次更改都是客户机偶然设置的日期值?如果可以包括操作实体并更新状态的服务器端代码,那也会有帮助。是的,客户可以设置日期,我正在设置与之前相同的日期。。而且,我在这里包含了服务器端代码,您可以先看一看,我认为这可能是TemporalType.DATE
和Envers不尊重这一点的问题,但事实并非如此。根据您所展示的内容,我唯一的结论是,在数据库中的字段值与客户端发送的字段值之间存在差异。你能拿出证据证明情况并非如此吗?请问您也在使用什么版本的Hibernate&Hibernate Envers?之前我认为相同,但db字段值与客户端值相同,我已经检查了多次。我使用的“hibernate-envers-5.2.4”与“hibernate jars版本”相同。这里,对于单击的每个保存按钮,它都在REVINFO和_Aud表中运行insert查询,而不检查更改(因为我认为它没有检查)。问题出在哪里,是否需要额外的Jarsthank来解释,现在我尝试了session#saveOrUpdate,但envers仍在为每次点击创建新的修订版,我只是更新数据,而不是插入新数据。当你说更新时,数据是否真的随着点击而改变?我有问题,感谢您的文档参考Naros。因此,我不得不使用session#merge,而不是session#saveOrUpdate。现在envers每次都在进行比较,若它发现了变化,那个么只需创建修订,现在应用程序运行良好。。voillaaa@V.DevsaveOrUpdate
和merge
afaik之间应该没有区别,这听起来像是一个bug。只是做了一些类似Domain Domain=session.find(Domain.class,domainId)
的测试,然后简单调用session.save(Domain)
,没有创建任何修订。因此,它一定是在您发送数据或处理数据的方式中,您没有指定的内容。无论我调用save
,saveOrUpdate
,update
,还是merge
,它们都是一样的。