-->
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.  [ 2 posts ] 
Author Message
 Post subject: Parent/Child - can't get cascade option correct for update
PostPosted: Mon Feb 04, 2008 6:12 pm 
Regular
Regular

Joined: Thu Nov 30, 2006 10:48 am
Posts: 59
I'm having problems getting the correct cascade option working for all CRUD (Create, Retrieve, Update, Delete) operations on a Parent/Child relationship. Save, Delete, and retrieve-by-id work but Update results in: "Illegal attempt to associate a collection with two open sessions". I've added inverse="true" to the Parent's Child collection and not-null="true" to the Child class mapping's Parent reference (to allow cascading deletes to work) and then tried different cascade options ("all", "all-delete-orphan", "save-update").

Parent has collection of Child; Child has reference to Parent. All Primary Keys are ID fields that are 'identity'. tblChild (db table) has FK_ParentID that is non-null. Following is the code (I showed each of the operations commented-out -- in testing, each one is performed by itself).

Parent parent = new Parent();
Child child = new Child();
child.ParentRef = parent;
parent.Children = child;
//session.SaveOrUpdate(parent); // save
//session.Get(Parent, parent); // retrieve-by-id
//session.Delete(parent); // delete
//session.SaveOrUpdate(parent); // update
session.Flush();

In the following, I changed cascade to "all", "all-delete-orphan", and "save-update" and in all cases save, retrieve-by-id, and delete work but update throws the exception:

<class name="Parent, Domain">
<bag name="Children" cascade="all" outer-join="true" inverse="true">
<key column="FK_ParentID"/>
<one-to-many class="Child, Domain"/>
</bag>
</class>

<class name="Child, Domain">
<many-to-one
name="ParentRef"
column="FK_ParentID"
class="Parent, Domain"
not-null="true"
/>
</class>

Thanks,
Bill

_________________
metazone


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 05, 2008 2:56 pm 
Regular
Regular

Joined: Thu Nov 30, 2006 10:48 am
Posts: 59
Note: this has been resolved -- see response to post titled "Illegal attempt to associate a collection with two open sess". I'll repeat it here:

I figured out the problem - in an effort to simplify the code snippet, the code snippet did not show what was exactly going on. I had built a DAL/ORM layer for my project w/ the former version being ADO (insulated the app from knowing whether nHib or ADO is being used). When I put in the nHib version, the session object is not managed as well as it should be.

Basically, I retrieve the Parent and then flush the session (but don't close it). I then create a new session (my f/w does) to perform the update. That's the problem -- the code snippet made it look like it was being done w/ one session and it wasn't. I read something on the web that said the exception occurred for a variety of reasons (and I then incorrectly assumed the exception msg was cryptic) and, because I read that one case was an invalid cascade option, I didn't following the right path for remediation. Thanks to all those who took a look at it.

Note: The Parent is a detached object which my friend says he uses session.Merge() in Hibernate -- not nHibernate -- to make it not detached -- note: nHib doesn't have this.

_________________
metazone


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.