Hibernate ConstraintViolationException:无法删除Child的SortedSet集合

时间:2012-05-31 14:08:24

标签: java hibernate treeset

我有以下两个类: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;
}

似乎问题出现在我使用SetHashSet)的映射中,但是如果我使用SortedSetTreeSet),它会给出一个问题错误。

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

1 个答案:

答案 0 :(得分:0)

行。问题现在解决了。在@JB Nizet的帮助下

对于同一个Claim我有几个ClaimInsurance compareTo()给出相同的结果。

我更改了compareTo()中的ClaimInsurance,以便它会为同一个Claim返回不同的值..就是这样,它现在正在工作。