-->
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.  [ 4 posts ] 
Author Message
 Post subject: Cascade saving objects in a One-to-One mapping
PostPosted: Sun Dec 21, 2003 9:14 am 
Newbie

Joined: Sun Nov 30, 2003 4:46 am
Posts: 17
Hello,

I have a class A that has a one-to-one relationship with class B

The mapping of class A is of the form:

<class name="A" table="A">
<id name="id" column="B_Id_FK" type="long">
<generator class="native"/>
</id>

<one-to-one name="bRef"
class="B"
constrained="true"
cascade="all"
/>
</class>

but class B doesnot have any reference to A. The mapping is thus unidirctional. Since I have
specified cascade=all thus when I'll create a new instance of both the classes and try to save
them by:
A a = new A();
B b = new B();
b.setBRef(b);
session.save(b);

I expect that both will be saved in the order that first A and then B.
But the result that I get is that 1st A gets saved and then B but Hibernate does not set the
foreign key B_Id_FK in Table B. Hence B gets saved with NULL in its foreign key.

The only way out of this problem seems to be that id generator of A be set to "assigned"
and 1st A be saved then assign A's id to B: b.setId(Aid)

Isn't there any way in Hibernate that I can save both the classes A and B in one go? Am I missing something here?

Thanx in advance for any suggestions and tips


Top
 Profile  
 
 Post subject:
PostPosted: Sun Dec 21, 2003 10:17 am 
Expert
Expert

Joined: Tue Sep 16, 2003 4:06 pm
Posts: 318
Location: St. Petersburg, Russia
Actually I do not understand why you expeca A will be saved if you use

Code:
session.save(b);


Association exists from A to B and not vice versa. Also, "cascade" is specified for A->B. So how do you expect Hibernate will know to save A?

I suppose you should be saving A and this will cause Hibernate to cascade and save B as well.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Dec 21, 2003 4:33 pm 
Newbie

Joined: Sun Nov 30, 2003 4:46 am
Posts: 17
Oops!
I reversed the objects for my scenarios.
Please re-consider my problem starting from the code:

A a = new A();
B b = new B();
a.setBRef(b);
session.save(a);

I expect that both will be saved in the order that first B and then A.
But the result that I get is that 1st B gets saved and then A but Hibernate does not set the
foreign key B_Id_FK in Table A. Hence A gets saved with NULL in its foreign key.

The only way out of this problem seems to be that id generator of A be set to "assigned"
and 1st B be saved then assign B's id to A: a.setId(Bid)

Isn't there any way in Hibernate that I can save both the classes A and B in one go? Am I missing something here?

Thanx in advance for any suggestions and tips[/b]


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 22, 2003 5:00 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
consider "foreign" generator

_________________
Emmanuel


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