-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: org.hibernate.HibernateException: A collection with cascade=
PostPosted: Fri Apr 21, 2006 10:06 am 
Beginner
Beginner

Joined: Thu Feb 23, 2006 11:22 am
Posts: 30
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 Param
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="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..


Top
 Profile  
 
 Post subject: perhaps this will help save-update works.
PostPosted: Fri Apr 21, 2006 12:45 pm 
Beginner
Beginner

Joined: Thu Feb 23, 2006 11:22 am
Posts: 30
Inside the NamedQuery.hbm.xml mapping file which I included in the first post changing the params set will cause it to work. But this setting will no remove nodes that I remove from the set. That is my understanding at least.
Code:
<set name="params" table="params" cascade="save-update">
   <key column="queryid"/>
   <one-to-many class="Param"/>
</set>

The generated query is as follows.. (note I removed 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=?

Hibernate:
    Select  nextval ('hibernate_sequence')
Hibernate:
    Select  nextval ('hibernate_sequence')
Hibernate:
    insert
    into
        Params
        (Name, Value, JavaClassNameString, queryID, params_id)
    values
        (?, ?, ?, ?, ?)
Hibernate:
    insert
    into
        Params
        (Name, Value, JavaClassNameString, queryID, params_id)
    values
        (?, ?, ?, ?, ?)
Hibernate:
    Update Params
    Set queryid=null
    where queryid=?
Hibernate:
    Update Params
    Set queryid=?
    Where params_id=?
Hibernate:
    Update  Params
    Set queryid=?
    Where params_id=?
Hibernate:
    Update  Params
    Set  queryid=?
    Where  params_id=?
transaction.commit()
addParamsToExistingNamedQuery Exit
   ---   Create HQL Query Add New Param Finished
ENDING HQL Manager
------ DONE ------


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 24, 2006 9:20 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
GoodDog - too verbose and...

If your posting (or a question you are referring to) was not answered by anybody, the possible reasons are:

- http://www.hibernate.org/ForumMailingli ... AskForHelp
- You did not submit enough information
- Nobody knows the answer or has the free time to answer

What you can do now:

- Do the things listed in After Posting
- Add missing and/or more information
- Consider commercial support for guaranteed expert response times

This is a high-traffic forum run by volunteers with hundreds of postings made every day. The community works because people try to help others in their free time. Nobody is paid for this service or has to pay.

You should not expect a timely response and you should not rely on a public community forum for critical cases.

All community members should respect the rules of this forum and treat others like they would prefer to be treated.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 24, 2006 9:37 am 
Beginner
Beginner

Joined: Thu Feb 23, 2006 11:22 am
Posts: 30
yeah it is pretty long.. Okay. it is WAY to long.. okay. Got it.. I will repost a shorter version. Just not real sure how to do it with out the mapping files and such. But that is my problem and I will figure something out. Hey Max, thanks.. Got to run now. Will post a new one by the end of the day..
Have a good day Max.. nice hearing from you again. :)


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 24, 2006 4:58 pm 
Beginner
Beginner

Joined: Thu Feb 23, 2006 11:22 am
Posts: 30
GOT IT..

First I want to thank Paul Curren for posting the solution. Nice work Paul.. He posted the soltion under pcurren titled "watch out for non-deep copy when hibernate processes lists".

Well you need to be careful with sets to..

I changed this

Code:
public void setParams(Set paramsToAdd) {
   Param param = null;
   Iterator paramsIter = paramsToAdd.iterator();
   while (paramsIter.hasNext()){
         setParams((Param)paramsIter.next());
   }
}


to this:

Code:
public void setParams(Set params) {
     this.params = params;
}


and it works great. Changed my old setParam to a different name so other parts could use that instead of sharing the setParams method..
once again thanks Paul..


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.