-->
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.  [ 5 posts ] 
Author Message
 Post subject: Cascading Save-Update in One to Many association
PostPosted: Mon Dec 01, 2003 7:14 am 
Newbie

Joined: Sun Nov 30, 2003 4:46 am
Posts: 17
In my understanding,

In case of a one to many bidirectional association, with inverse set to true and foreign key set to NOT NULL. If saving the parent is to be cascaded to its children then all is needed is setting the parent pointer in child, adding child to parent and then inserting parent. Inserting parent will then be cascaded to its children which will also be inserted. Thats well and good for a bidirectional association.

My problem is with the case of a one to many association that is not bidirectional, with inverse set to false and foreign key set to NOT NULL, and saving the parent is to be cascaded to its children. Now when I add child to parent and then insert parent Hibernate throws the SQLException that
Quote:
Cannot insert NULL in foreign key column in Child table


When at the application level the child has been added to parent then why doesnt Hibernate insert the foreign key while inserting child record. I guess the pattern Hibernate follows is: Insert Child record and then update Child record with the foreign key. Why cant all this be done in one single Child record insert in which the foreign key is also inserted

Is there a way around this problem?
Thanx in advance for suggestions and tips


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 01, 2003 8:05 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
You may not use a unidirectional association for a column with a NOT NULL constraint.


Top
 Profile  
 
 Post subject: Got it!
PostPosted: Mon Dec 01, 2003 8:58 am 
Newbie

Joined: Sun Nov 30, 2003 4:46 am
Posts: 17
Thanx a alot!

I see that if I allow NULL for FK and set inverse to false then Hibernate appropriately inserts : Insert Parent, Insert Child and then update Child with FK

but if I set inverse to true then Hibernate insert Parent, inserts Child but doesnt update Child with FK

Thanx once again for ur advice!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 01, 2003 9:10 am 
Beginner
Beginner

Joined: Wed Nov 26, 2003 11:53 am
Posts: 26
Location: Netherlands
gavin wrote:
You may not use a unidirectional association for a column with a NOT NULL constraint.


Gavin, why is this so? Is this a Hibernate thing or is there a principle here that I am not familiar with? I think that this statement is pretty important: I didn't see it in the documentation.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 01, 2003 9:15 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
It is actually written in the doco in a couple of places.

The reason it is like this is that Hibernate

(a) prefers generation of SQL strings at config time
(b) supports detached objects

we consider both of these things as more important than supporting not-null unidirectional associations.

And its pretty much a case of "we can't have all three".


(There are *some* things we could do to try and improve it a bit but they are ugly.)


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