我有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;
}
然后我也遇到了错误。 我还能做些什么来解决这个错误。 感谢
答案 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);
}