java HibernateException:拥有级联的实体实例不再引用带有级联 =“all-delete-orphan”的集合

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38524683/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 03:29:16  来源:igfitidea点击:

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

javahibernatehibernate-mapping

提问by Nalini Wanjale

I have PolicyDO and PolicyDocumentDO.relation between them is as follows

我有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>  

When ever I tried to query something from database like below

当我尝试从数据库中查询如下内容时

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

I get following error

我收到以下错误

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

dataobjects.policy.PolicyDO.listPolicyDocumentDOList

dataobjects.policy.PolicyDO.listPolicyDocumentDOList

So after googling I did following changes while setting listPolicyDocumentDOList in PolicyDO

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

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

Then also I am getting above error. What else I can do to solve this error. Thanks

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

回答by Tomasz Fija?kowski

change

改变

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

to

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