HibernateException:拥有实体实例不再引用具有cascade =“all-delete-orphan”的集合

时间:2016-07-22 10:54:37

标签: java hibernate hibernate-mapping

我有PolicyDO和它们之间的PolicyDocumentDO.relation如下

PolicyDO.hbm.xml

<bag name="listPolicyDocumentDOList" cascade="all-delete-orphan" lazy="false"   inverse="true">
            <key column="POLICYSEQ" />
            <one-to-many class="dataobjects.policy.PolicyDocumentDO" />

PolicyDO.java
protected List<PolicyDocumentDO> policyDocumentDOList = new ArrayList<PolicyDocumentDO>();
 public java.util.List<PolicyDocumentDO> getListPolicyDocumentDOList() {
    return this.policyDocumentDOList;
  }

  public void setListPolicyDocumentDOList(java.util.List<PolicyDocumentDO> list) {
      policyDocumentDOList.clear();
      policyDocumentDOList = list;
  }


    PolicyDocumentDO.hbm.xml

    <many-to-one name="parentGuidObj" class="dataobjects.policy.PolicyDO"  not-null="true" >
            <column name="POLICYSEQ"  />
    </many-to-one>  

我什么时候尝试从数据库中查询某些内容,如下所示

session = sessionFactory.openSession();
Query query = session.createQuery(strBuff.toString());
List listQuery = query.list();

我收到以下错误

org.hibernate.HibernateException: A collection with cascade="all-delete-orphan" was no longer referenced by the owning entity instance: 

dataobjects.policy.PolicyDO.listPolicyDocumentDOList

所以在谷歌搜索之后我在PolicyDO中设置listPolicyDocumentDOList时做了以下更改

public void setListPolicyDocumentDOList(java.util.List<PolicyDocumentDO> list) {
      policyDocumentDOList.clear();
      policyDocumentDOList = list;
  }

然后我也遇到了错误。 我还能做些什么来解决这个错误。 感谢

1 个答案:

答案 0 :(得分:5)

变化

public void setListPolicyDocumentDOList(java.util.List<PolicyDocumentDO> list) {
    policyDocumentDOList.clear();
    policyDocumentDOList = list;
}

public void setListPolicyDocumentDOList(java.util.List<PolicyDocumentDO> list) {
    policyDocumentDOList.clear();
    policyDocumentDOList.addAll(list);
}