我有以下两个类: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),则会出现错误。

实际问题可能是什么?我想念什么?

最佳答案

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

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

我更改了compareTo()中的ClaimInsurance,以便它将为相同的Claim返回不同的值。就是这样,它现在可以工作了。

关于java - Hibernate ConstraintViolationException:无法删除子项的SortedSet集合,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10835293/

10-09 05:54