cascade =“all-delete-orphan”不再被引用

时间:2014-06-17 14:14:12

标签: java spring hibernate java-ee

我一直在这里看到有关此异常但我无法找到解决方案,所以我想只是粘贴我的代码以防万一有问题。 我在尝试查找EvaluationSelectionCriteria实体时收到此异常。

Exception: org.hibernate.HibernateException: A collection with cascade="all-delete-      orphan" was no longer referenced by the owning entity instance: com.greenvalley.etendering.domain.Candidacy.evaluationSelectionCriterias 


 @OneToMany(cascade = {CascadeType.ALL}, fetch = FetchType.LAZY)
@JoinColumn(name = "candidacy_id", nullable = false)
@XmlElement
@JsonIgnore
@Getter
@Setter
private List<EvaluationSelectionCriteria> evaluationSelectionCriterias = new ArrayList<>();



@ManyToOne
@JoinColumn(name = "candidacy_id", nullable = false, insertable = false, updatable = false)
@XmlTransient
@Getter
@Setter
private Candidacy candidacy;

2 个答案:

答案 0 :(得分:3)

我通过用这样的

替换我的setXX方法解决了这个问题
public void setEvaluationSelectionCriterias(List<EvaluationSelectionCriteria> evaluationSelectionCriterias) {

if(this.evaluationSelectionCriterias == null) { //if not already initialized
  this.evaluationSelectionCriterias = evaluationSelectionCriterias;
} else {
  this.evaluationSelectionCriterias.clear();
  this.evaluationSelectionCriterias.addAll(evaluationSelectionCriterias);
}

答案 1 :(得分:1)

我发现在处理@ManyToOne关系时@JoinTable似乎比@JoinColumn更好。尝试这样的事情:

@OneToMany(cascade = {CascadeType.ALL}, fetch = FetchType.LAZY)
@JoinTable(
   name="candidacy_evaluation_search_criteria",
   joinColumns={ @JoinColumn(name="evaluation_selection_criteria", referencedColumnName="id") },
   inverseJoinColumns={ @JoinColumn(name="candidacy", referencedColumnName="id", unique=true) })
@XmlElement
@JsonIgnore
@Getter
@Setter
private List<EvaluationSelectionCriteria> evaluationSelectionCriterias = new ArrayList<>();

@ManyToOne(mappedBy = "evaluationSelectionCriterias")
@XmlTransient
@Getter
@Setter
private Candidacy candidacy;

作为旁注,&#39;标准&#39;是多个&#39;标准&#39;,因此您的班级名称应为EvaluationSelectionCriterion,集合名称为evaluationSelectionCriteria