org.hibernate.HibernateException: A collection with cascade="all-delete-orphan”
HIBERNATE VERISON:
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.6.5
Created-By: 1.4.2_09-b05 (Sun Microsystems Inc.)
Hibernate-Version: 3.1.2
THE MAPPING FILES:
This is the parent object:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="hibernate.pojo">
<class name="NamedQuery" table="namedquery">
<id name="queryid" type="long" column="queryid">
<generator class="native">
<param name="native">queryid</param>
</generator>
</id>
<property name="QueryName" type="string" column="QueryName" not-null="true" />
<property name="Description" type="string" column="Description"/>
<property name="QueryString" type="string" column="QueryString"/>
<property name="hqlType" type="java.lang.Boolean" column ="hqlType"/>
<property name="Deprecated" type="java.lang.Boolean" column="Deprecated"/>
<set
name="params" table="params" cascade="all-delete-orphan">
<key column="queryid"/>
<one-to-many class="Param"/>
</set>
<!-- define the FK for the Mapping File object -->
<!-- note: this is actually a one-to-one mapping -->
<many-to-one name="mappingFile" column="mappingFileID" class="MappingFile" cascade="all" unique="true" />
<many-to-one name="author" column="authorid" class="Author" cascade="all" unique="true" />
</class>
</hibernate-mapping>
This is the child object called ParamCode:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="hibernate.pojo">
<class name="Param" table="Params">
<id name="params_id" column="params_id" type="long">
<generator class="native">
<param name="native">params_id</param>
</generator>
</id>
<property name="Name" column="Name" type="java.lang.String" />
<property name="Value" column="Value" type="java.lang.String" />
<property name="JavaClassNameString" column="JavaClassNameString" type="java.lang.String" />
<!-- <property name="queryID" column="queryID" type="java.lang.Integer" /> -->
<many-to-one name="namedQuery" column="queryID" class="NamedQuery" not-null="true" />
</class>
</hibernate-mapping>
CALLER:
System.out.println("\t--- Create HQL Query Add New Param Started");
String paramDelim= Param.PARAM_TUPLE_DELIM;
String queryName = "testQueryName";
List paramStringsToAdd = new Vector();
paramStringsToAdd.add("ParamName1" + paramDelim + "ParamName1 Value" + paramDelim + "java.lang.String");
paramStringsToAdd.add("ParamName2" + paramDelim + "ParamName2 Value" + paramDelim + "java.lang.String");
Session session = HibernateSessionFactory.currentSession();
hqlQueryTester.addParamsToExistingNamedQuery(session, queryName, paramStringsToAdd);
HibernateSessionFactory.closeSession();
System.out.println("\t--- Create HQL Query Add New Param Finished");
CALLS THIS METHOD
public void addParamsToExistingNamedQuery(Session session, String queryName,
List paramStringsToAdd){
System.out.println("addParamsToExistingNamedQuery Entered");
String HQLString = "from NamedQuery namedQry where namedQry.QueryName = :searchName";
System.out.println(HQLString);
Transaction transaction = session.beginTransaction();
NamedQuery namedQuery = (NamedQuery)session.createQuery(HQLString)
.setString("searchName", queryName)
.setMaxResults(1)
.uniqueResult();
System.out.println("Returned Query=>" + namedQuery.toString(true));
ouputParams("before setParams", namedQuery.getParams());
namedQuery.setParams(paramStringsToAdd);
ouputParams("after setParams", namedQuery.getParams());
System.out.println("setParams(paramStringsToAdd)");
transaction.commit();
System.out.println("transaction.commit()");
System.out.println("addParamsToExistingNamedQuery Exit");
}
This code successfully retrieves the namedQuery object. Exception happens when hibernate attempts to commit the two newly added Param objects. Breakpoint in the debugger right before commit shows the two Param objects I added to the NamedQuery Param Set with id’s set to null and pointing back to the NamedQuery object. Id’s should be null since they are yet persisted.
I have a bidirectional association from NamedQuery to Param. So NamedQuery points to one or more Params and each Param points back to it’s parent NamedQuery object.
The NamedQuery object has several setParam methods which are as follows:
Code:
public void setParams(List paramStrings) {
this.setParams(createParamsSet(paramStrings));
}
public void setParams(Set paramsToAdd) {
Param param = null;
Iterator paramsIter = paramsToAdd.iterator();
while (paramsIter.hasNext()){
setParams((Param)paramsIter.next());
}
}
public void setParams(Param paramToAdd) {
this.params.add(paramToAdd);
paramToAdd.setNamedQuery(this);
}
here is the SQL that Hibernate Generates and then Exception that I am getting. Note is also contains some out my debugging output:
Code:
addParamsToExistingNamedQuery Entered
from NamedQuery namedQry where namedQry.QueryName = :searchName
Hibernate:
select
namedquery0_.queryid as queryid0_,
namedquery0_.QueryName as QueryName0_,
namedquery0_.Description as Descript3_0_,
namedquery0_.QueryString as QueryStr4_0_,
namedquery0_.hqlType as hqlType0_,
namedquery0_.Deprecated as Deprecated0_,
namedquery0_.mappingFileID as mappingF7_0_,
namedquery0_.authorid as authorid0_
from
namedquery namedquery0_
where
namedquery0_.QueryName=? limit ?
Hibernate:
select
params0_.queryid as queryid1_,
params0_.params_id as params1_1_,
params0_.params_id as params1_2_0_,
params0_.Name as Name2_0_,
params0_.Value as Value2_0_,
params0_.JavaClassNameString as JavaClas4_2_0_,
params0_.queryID as queryID2_0_
from
Params params0_
where
params0_.queryid=?
Hibernate:
select
author0_.authorid as authorid3_1_,
author0_.Name as Name3_1_,
namedquery1_.queryid as queryid0_0_,
namedquery1_.QueryName as QueryName0_0_,
namedquery1_.Description as Descript3_0_0_,
namedquery1_.QueryString as QueryStr4_0_0_,
namedquery1_.hqlType as hqlType0_0_,
namedquery1_.Deprecated as Deprecated0_0_,
namedquery1_.mappingFileID as mappingF7_0_0_,
namedquery1_.authorid as authorid0_0_
from
Authors author0_
left outer join
namedquery namedquery1_
on author0_.authorid=namedquery1_.mappingFileID
where
author0_.authorid=?
Hibernate:
select
mappingfil0_.mappingFileId as mappingF1_1_1_,
mappingfil0_.packageName as packageN2_1_1_,
mappingfil0_.fileName as fileName1_1_,
mappingfil0_.version as version1_1_,
namedquery1_.queryid as queryid0_0_,
namedquery1_.QueryName as QueryName0_0_,
namedquery1_.Description as Descript3_0_0_,
namedquery1_.QueryString as QueryStr4_0_0_,
namedquery1_.hqlType as hqlType0_0_,
namedquery1_.Deprecated as Deprecated0_0_,
namedquery1_.mappingFileID as mappingF7_0_0_,
namedquery1_.authorid as authorid0_0_
from
MappingFile mappingfil0_
left outer join
namedquery namedquery1_
on mappingfil0_.mappingFileId=namedquery1_.mappingFileID
where
mappingfil0_.mappingFileId=?
Hibernate:
select
namedquery0_.queryid as queryid0_0_,
namedquery0_.QueryName as QueryName0_0_,
namedquery0_.Description as Descript3_0_0_,
namedquery0_.QueryString as QueryStr4_0_0_,
namedquery0_.hqlType as hqlType0_0_,
namedquery0_.Deprecated as Deprecated0_0_,
namedquery0_.mappingFileID as mappingF7_0_0_,
namedquery0_.authorid as authorid0_0_
from
namedquery namedquery0_
where
namedquery0_.mappingFileID=?
Returned Query=>[NamedQuery:queryid=>100<
QueryName=>testQueryName<
Description=>description for this query<
QueryString=>from NamedQuery nQuery where nQuery.QueryName=:queryName<
hqlType=>true<
Deprecated=>true<]Author=>[Params:
authorid=>102
Name=>author]<
[Params=>(1) entries
[Params:
params_id=>103
Name=>queryName
Value=>query Name
JavaClassNameString=>null
namedQuery=>100]
]<
MappingFile=>[MappingFile:
mappingFileId=>101
packageName=>hibernate.pojo
fileName=>NamedQuery.hbm.xml
version=>null
]<]
setParams(paramStringsToAdd)
Hibernate:
select
nextval ('hibernate_sequence')
Hibernate:
select
nextval ('hibernate_sequence')
Exception in thread "main" org.hibernate.HibernateException: A collection with cascade="all-delete-orphan" was no longer referenced by the owning entity instance: hibernate.pojo.NamedQuery.params
at org.hibernate.engine.Collections.processDereferencedCollection(Collections.java:96)
at org.hibernate.engine.Collections.processUnreachableCollection(Collections.java:39)
at org.hibernate.event.def.AbstractFlushingEventListener.flushCollections(AbstractFlushingEventListener.java:217)
at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:77)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:26)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1009)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:356)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at source.HQLQueryTester.addParamsToExistingNamedQuery(HQLQueryTester.java:88)
at source.Driver.main(Driver.java:71)
I have done this type of thing many times but I just can not seem to get past this issue, I need another set of eyes. Can someone take a look at this for me.
Thanks..