-->
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.  [ 10 posts ] 
Author Message
 Post subject: Saving transient parent with transient children
PostPosted: Tue Nov 02, 2004 2:53 pm 
Newbie

Joined: Tue Sep 28, 2004 2:01 pm
Posts: 17
I have one parent with many children using the save-update cascade option children. Saving the transient parent with its transient children works but no foreign key from the child back to the parent was being stored. I assumed I had to call child.setParent( parent ) on each child to fix the problem. I tried that and got the following error:

"object references an unsaved transient instance - save the transient instance before flushing: foo.bar.Parent"

Do I need to save the parent first and add the children later?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 02, 2004 3:32 pm 
Newbie

Joined: Tue Sep 28, 2004 2:01 pm
Posts: 17
Anthony posted this response to a recent question related to mine. I am doing the child.setParent( parent ) and parent.addChild( child ) thing but outside of a session. I expect hibernate to persist the parent first followed with the children but instead I get the "unsaved transient" error.

*********************

Transaction trx = session.beginTransaction();

Parent stephen = new Parent();
stephen.setName("Stephen Earl");
session.save(stephen);

Child simon = new Child();
simon.setName("Simon Earl");
stephen.addChild(simon);
simon.setParent(stephen);

trx.commit();


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 02, 2004 4:28 pm 
Expert
Expert

Joined: Thu Jan 29, 2004 2:31 am
Posts: 362
Location: Switzerland, Bern
either do
Code:
session.save(simon);

befor commiting

od specify cascade="save-update".

Referende manual chapter 16 describes this in detail.

HTH
Ernst


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 02, 2004 5:03 pm 
Newbie

Joined: Tue Sep 28, 2004 2:01 pm
Posts: 17
ernst_pluess wrote:
either do
Code:
session.save(simon);

befor commiting

od specify cascade="save-update".

Referende manual chapter 16 describes this in detail.

HTH
Ernst


Understood but that was a quote from a working example and I believe they were using save-update. I am using save-update as you suggest so all's well on that front.

My question is whether you can create a new parent with new children and save the tree with session.save( parent ) or whether the parent must fist be persisted.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 02, 2004 5:23 pm 
Newbie

Joined: Tue Sep 28, 2004 2:01 pm
Posts: 17
I think my question is going off in the weeds so here is a course correction:

Persisting a populated parent works. Examining the database reveals the child records have a null parent reference. This is because the parent was not set for each item so the back-reference is missing.

To fix the problem I call child.setParent( parent ) on each child. The problem is the parent is transient (not unattached but rather entirely new) and so are all of the child objects. This tree is created and used long before it is stored in the database. That is how the application is designed.

Now, when the time comes to persist it I get the reference to an unsaved object complaint. This begs the question, can a tree be saved in one fell swoop or not? Must the parent be saved first if the children have a back reference to the parent?

Thx


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 03, 2004 3:03 am 
Expert
Expert

Joined: Thu Jan 29, 2004 2:31 am
Posts: 362
Location: Switzerland, Bern
AFAIK the parent must be saved first.

If you speciefy cascade="save-update" that's all you have to do, else save the children as well.

HTH
Ernst


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 03, 2004 1:11 pm 
Newbie

Joined: Tue Sep 28, 2004 2:01 pm
Posts: 17
That would mean you could not create a tree and save it without some code gymnastics. Customer creates an order and adds a pizza with toppings. The order is then sent over to the persistence manager where the elaborate dance is implemented: remove pizza, remove toppings, persist order, persist pizza, update pizza with toppings, update order with pizza. Yikes!


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 03, 2004 2:47 pm 
Newbie

Joined: Tue Sep 28, 2004 2:01 pm
Posts: 17
Here's a solution. Pretty simple and only unnatural in that you can't use the back references from, say, the pizza to its order. Not a big deal really.

// Create a POJO order independent of storage. Just beware that the
// pizza.getOrder() will be null and topping.getPizza() will be null.

Order create()
{
Order order = new Order();
Pizza pizza = new Pizza();
Topping cheese = new Mozarrella();
Topping meat = new Pepperoni();

pizza.add( cheese );
pizza.add( meat );
order.add(pizza);

return order;
}

// Save the order in two steps: without links then with links. Otherwise
// Hibernate whines about transient objects being referenced.

store( order )
{
session.save( order );

cheese.setPizza( pizza );
meat.setPizza( pizza );
pizza.setOrder( order );
storageManager.store( order );

session.update( order );
}


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 03, 2004 3:11 pm 
Expert
Expert

Joined: Thu Jan 29, 2004 2:31 am
Posts: 362
Location: Switzerland, Bern
Mmmm ... I guess you didn't unerstand how to work with persistent objects in hibernate.

Read chapter 4 of Hibernate in Action. After that have a look at chapter 16 of the reference manual to understand parent/child relations.

HTH
Ernst


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 03, 2004 3:51 pm 
Newbie

Joined: Tue Sep 28, 2004 2:01 pm
Posts: 17
ernst_pluess wrote:
Mmmm ... I guess you didn't unerstand how to work with persistent objects in hibernate.

Read chapter 4 of Hibernate in Action. After that have a look at chapter 16 of the reference manual to understand parent/child relations.

HTH
Ernst


I would say that I understood those secions entirely but that it failed in a way that is not described in HIA or the reference manual. In fact you do not have to store the parent first and that much is documented. My question was slightly more specific. If a child has a parent reference and both are transient to you have to store the parent first? In that case, and only that case, the answer appears to be affirmative but I am still not sure. I thought perhaps there was a way that Hibernate could deal with this circular reference since it's entirely detectable and have still not received a clear answer on whether that's possible. Pretty much down with how hibernate is working otherwise.

Thanks.


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