2

我有以下两个类:(Claim父)和ClaimInsurance(子)。它们如下:

public class Claim {    
  private SortedSet<ClaimInsurance> claimInsurances = new TreeSet<ClaimInsurance>();

  @OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY, mappedBy="claim", orphanRemoval=true)
  @Sort(type=SortType.NATURAL)
  public SortedSet<ClaimInsurance> getClaimInsurances() {
    return this.claimInsurances;
  }

  public void setClaimInsurances(SortedSet<ClaimInsurance> claimInsurances) {
    this.claimInsurances = claimInsurances;
  }
}

和:

public class ClaimInsurance implements java.io.Serializable, Comparable<ClaimInsurance> {
  private Claim claim;

  @ManyToOne(fetch=FetchType.LAZY)
  @JoinColumn(name="ClaimId", nullable=false)
  public Claim getClaim() {
    return this.claim;
  }

  public void setClaim(Claim claim) {
    this.claim = claim;
  }
}

当我尝试删除Claim它时,它会给出以下异常

org.hibernate.exception.ConstraintViolationException: could not delete: [com.omnimd.pms.beans.Claim#201]
...
Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: The DELETE statement conflicted with the REFERENCE constraint "FK_RCMSClaimInsuranceTable_RCMSClaimTable". The conflict occurred in database "Omnimdv12", table "dbo.RCMSClaimInsuranceTable", column 'ClaimId'.

当我如下更改类中的claimInsurances映射时Claim,一切正常:

private Set<ClaimInsurance> claimInsurances = new HashSet<ClaimInsurance>();

@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY, mappedBy="claim", orphanRemoval=true)
public Set<ClaimInsurance> getClaimInsurances() {
  return this.claimInsurances;
}

public void setClaimInsurances(Set<ClaimInsurance> claimInsurances) {
  this.claimInsurances = claimInsurances;
}

似乎问题是当我在映射中使用Set( HashSet) 时它可以工作,但如果我改为使用SortedSet( TreeSet) 则会出错。

实际问题可能是什么?我错过了什么?

4

1 回答 1

0

好的。现在问题已经解决了。在@JB Nizet 的帮助下

同样Claim,我有几个给出相同ClaimInsurancecompareTo()结果。

我改变了compareTo()inClaimInsurance这样它会为相同的返回不同的值Claim..就是这样,它现在可以工作了。

于 2012-06-01T06:18:40.497 回答