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.  [ 6 posts ] 
Author Message
 Post subject: Newbie - Creating child record when parent not yet persisted
PostPosted: Wed Dec 09, 2009 4:24 pm 
Newbie

Joined: Wed Dec 09, 2009 4:12 pm
Posts: 3
What is the preferred method for handling when you would like to create a parent object, Request, and several children objects, SubRequests, without having to save any objects until the user decides to?

I'm using nHibernate 2.0.1 and Oracle in an ASP.NET environment. The objects are loaded and then stored in the ASP.Net session until the user either saves or cancels any changes.

Is there a way to capture the Request's ID (or SubRequest's) from the sequence without actually saving the object? Should I be manually getting the next sequence value and populating it in the object in this case? Is there another method that is better?

Thanks


Top
 Profile  
 
 Post subject: Re: Newbie - Creating child record when parent not yet persisted
PostPosted: Thu Dec 10, 2009 2:23 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
For what do you need the id ? If you mapped your objects in the normal way you just do this:

- create parent
- create child
- add child to parent
- repeat .....
- .....

- Save everything

_________________
--Wolfgang


Top
 Profile  
 
 Post subject: Re: Newbie - Creating child record when parent not yet persisted
PostPosted: Thu Dec 10, 2009 10:26 am 
Newbie

Joined: Wed Dec 09, 2009 4:12 pm
Posts: 3
In the constructor of the subrequest I am passing in the foreign key value (the ID of the request) which since it is not saved is 0. When I eventually save both, will the ID of the Parent get resolved (by a Oracle sequence generator) and passed along to the child's foreign key field?

Parent p = new Parent(); // id = 0 until saved and set by Oracle sequence
Child c = new Child(p.ID); // both Parent and Child id is 0
p.Children.Add(c); // does not work, p.Children.Count = 0?????

Both have id's mapped like this

<generator class="native">
<param name="sequence">arfadm.sr_seq</param>
</generator>

The child collection is mapped as

<bag name="SubRequests" table="sub_request" cascade="all-delete-orphan" inverse="true">
<key column="sr_id" on-delete="cascade" />
<one-to-many class="SubRequest" />
</bag>

The above scenario works fine only if I save the Parent (populate ID from the sequence) before creating the Child?

Thanks,


Top
 Profile  
 
 Post subject: Re: Newbie - Creating child record when parent not yet persisted
PostPosted: Thu Dec 10, 2009 10:30 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Child c = new Child(p.ID); // both Parent and Child id is 0


Why do you need this ? Do you have a back reference from child to parent ? Have a look here:

http://nhforge.org/doc/nh/en/index.html#example-parentchild

Normally you would do this:

Parent p = new Parent();
Child c = new Child();
p.Children.Add(c);
c.Parent = p;

There's no need for knowing the id. You can just save the bunch of objects and hibernate will take careof the ids.

_________________
--Wolfgang


Top
 Profile  
 
 Post subject: Re: Newbie - Creating child record when parent not yet persisted
PostPosted: Thu Dec 10, 2009 11:10 am 
Newbie

Joined: Wed Dec 09, 2009 4:12 pm
Posts: 3
Ahhh...The issue was design. My child class did not have a Parent member, just an int representing a Parent_ID member so nHibernate could not resolve that properly.

Thanks


Top
 Profile  
 
 Post subject: Re: Newbie - Creating child record when parent not yet persisted
PostPosted: Thu Dec 10, 2009 11:17 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
If you define the bag as inverse, you need the reference from child to parent !!

_________________
--Wolfgang


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