-->
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: Get the ID of a Child in a cascade="all" relationship
PostPosted: Tue Apr 06, 2010 9:26 pm 
Newbie

Joined: Fri Jan 29, 2010 8:37 am
Posts: 5
Hi,
i have two Entities, "Parent" and "Child", that are linked through a bidirectional one-to-many relationship with the cascade attribute set to "all".
When adding a Child object to the Parent children collection using the code below, i can't get the ID of the persisted child until i commit the transaction:

Code:
Parent p = (Parent) session.load(Parent.class, pid);
Child c = new Child();
p.addChild(c);
// "c" hasn't an ID (is always zero)

However, when i persist a child entity by explicitly calling the session.save() method, the ID is created and set immediately, even if the transaction hasn't been committed:

Code:
Child c = new Child();
session.save(c);
// "c" has an ID

Is there a way to get the ID of the child entity immediately without calling the session.save() method?

Thanks


Top
 Profile  
 
 Post subject: Re: Get the ID of a Child in a cascade="all" relationship
PostPosted: Wed Apr 07, 2010 4:49 am 
Regular
Regular

Joined: Wed Mar 10, 2010 4:48 am
Posts: 106
lotstyl wrote:
Code:
Parent p = (Parent) session.load(Parent.class, pid);
Child c = new Child();
p.addChild(c);
// "c" hasn't an ID (is always zero)

Do you need the id for something else, because I think the code above would work if you save it like:
Code:
session.save(c);
session.update(p);


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.