对于每次保存(更新),hibernate envers 创建修订在 table 中更改或不更改

For each save (update) hibernate envers creating revisions either changed in table or not

在我的客户端服务器应用程序中,我使用 JavaFx 作为客户端并使用 Java hibernate 作为服务器到数据库连接。

问题

我有一个保存按钮,每次点击按钮,envers 创建一个修订,无论 table 值是否有变化。 基本上它不是将旧数据检查为新数据,只是创建修订。

这是我的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#save 的电话上。在 Session here 的 javadoc 中,您会注意到以下段落:

save() and persist() result in an SQL INSERT, delete() in an SQL DELETE and update() or merge() in an SQL UPDATE. Changes to persistent instances are detected at flush time and also result in an SQL UPDATE. saveOrUpdate() and replicate() result in either an INSERT or an UPDATE.

所以您基本上想使用 session#saveOrUpdate 以便根据实体的状态获得插入和更新语义。

由于 session#save 正在生成一个新的 INSERT 操作,无论如何,Envers 总是会为该案例创建一个新修订版。