-->
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: Is it possible to create whole object graph at once?
PostPosted: Sun Jun 19, 2005 1:24 am 
Newbie

Joined: Wed Jun 15, 2005 4:58 am
Posts: 6
Hibernate version:
3.0.5

Given a simple Parent/Child relationship (like the one in chapter 22 of the manual), is it possible to set up such a mapping that would allow to create both parent and child before saving both of them at the same time?

In other words a mapping that would allow for this code:
Code:
Parent p = new Parent();
Child c = new Child();
p.getChildren().add(c);
c.setParent(p);
session.merge(p);


Logically, I would think that the mappings in section 22.3 of the manual should work. But instead I get "not-null property references a null or transient value" exception.
(From doing some instrumenting, it appears that Hibernate calls 'c.setParent(null) when it tries to do the 'session.merge()'... Why? Does it think that since Parent is transient (still), it should set the child's parent reference to null to prevent writing garbage to the DB? Why doesn't it first save Parent, get generated ID value and the save Child?..)

This code can clearly be made to work by having two calles to 'merge':
Code:
Parent p = new Parent();
session.merge(p);
Child c = new Child();
p.setChildren().add(c);
c.setParent(p);
session.merge(p);


The "problem" with this approach (in my case) is that Parent cannot logically exist w/o its children, and new children cannot be added arbitrarily - so I wanted to make constructor for Child package scoped (while still separating "model" code from "hibernate" code in separate packages).

Any solutions?


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jun 19, 2005 2:55 am 
Newbie

Joined: Wed Jun 15, 2005 4:58 am
Posts: 6
I solved my problem.
The issue is use of 'merge'. If instead I use 'saveOrUpdate' everything works fine.
(Perhaps it would be worth while making this more pronounced in the documentation? In retrospect, it is certainly covered in the last sentence of section 11.7...)

Thank you.


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.