我有以下两个类:(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
) 则会出错。
实际问题可能是什么?我错过了什么?