-->
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: Saving modified list of objects
PostPosted: Sun Mar 13, 2011 8:49 am 
Newbie

Joined: Sun Mar 13, 2011 8:28 am
Posts: 2
I tried searching this but did not get an answer and hence posting it. If this is repeat question, my apologies.

I have a one to many relationship between two classes. Order class has a IList<OrderLine> and in the database there are two tables Order and OrderLine corresponding to these classes. Order object is created by NHibernate along with the list of order lines populated (lazy = false). Let's say there are three order lines #1, #2 and #3, to start with. Now, line #4 is added and #1 got dropped. So, I call clear() of IList<OrderLine> and insert the lines that are relevant now back into IList, after the change, which are #2, #3 and #4. Then I call the session.Update() for the order object. My expectation is it will delete the record in OrderLine table for #1 and insert #4.

In the order.hbm.xml, if I set cascade="all-delete-orphan" and inverse="false" (or just leave the inverse attribute out), it tries to update the Order ID of all the OrderLine records as null. Since database will not allow this (NOT NULL column), it fails. Whatever I do, I couldn't make it issue DELETEs. Of course by setting, inverse="true" in Order.hbm.xml and then calling individual deletes on OrderLine (session.Delete(orderLine)), I can accomplish what I want.

But I'm 100% sure, if every thing done correctly, NHibernate itself will do the deletion of records in OrderLine. But I couldn't get it to work. In the Order.hbm.xml, I have cascade set to all-delete-orphan and inverse="false". I believe only these two settings will do the trick but it is not to be. Am I wrong with the way I'm clearing the collection and creating the orderline objects and adding them back? Any pointers from the experts will be appreciated.


Top
 Profile  
 
 Post subject: Re: Saving modified list of objects
PostPosted: Mon Mar 14, 2011 3:00 am 
Newbie

Joined: Sun Mar 13, 2011 8:28 am
Posts: 2
Figured out the issue. Just posting here for other's benefit. I'm loosing the reference to IList<> and just recreating it myself and because of that, cascading is not happening. If I work off the original IList<> and add and remove, it does delete the child records correctly.


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.