Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

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

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

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

like image 293
Nalini Wanjale Avatar asked Jul 22 '16 10:07

Nalini Wanjale


1 Answers

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);
}
like image 183
Tomasz Fijałkowski Avatar answered Nov 08 '22 07:11

Tomasz Fijałkowski