-->
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: Insert order gotcha
PostPosted: Fri Jun 13, 2008 3:43 am 
Newbie

Joined: Mon Nov 19, 2007 9:01 am
Posts: 8
A little trick with insert order that might be interesting to some. Inserts are normally done in the order that objects are added to the persistence context. However, cascading can "promote" an insertion to earlier than you might think.

Say I have:

A --> C
B --> C

Where --> is a cascading one-to-many relationship. C is obviously a join table.

Then I do:

add A
add B
add C to both A and B

The inserts do NOT happen in this order:

A
B
C

What really happens is:

insert A (and cascade to C)
insert B (and cascade to C - but find we don't need to)
insert C (Oh, don't need to)

so the real order is:

A
C
B

If B --> C is not nullable, this will crash on the second insert.

To get around this, do either:

add A
add B
flush
add C to both A and B

or:

add B
add A
add C to both A and B

or even (not sure why this works):

add A
add B
add C to both A and B
session.save(B)


Kinda obvious when you think about it - not obvious when you don't!


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jun 14, 2008 8:55 am 
Regular
Regular

Joined: Sun Apr 13, 2008 3:04 am
Posts: 71
Location: Bangalore
Yep.. I would have wished Hibernate to have the intelligence to do all this with out user flusing etc.. as it could clearly know the relationships clearly..

To me ORM impl. still seem to be in very infant stage as one still needs to many things on db and it is not enought to represent his needs in true Objects.

B.T.W do u know how is the behaviour for map kind of containment.. trying to store Map which key and value as custom objects. However unless I use the previously stored key, the map is not getting stored.

Regards,
Nagendra

_________________
Raja Nagendra Kumar,
C.T.O
http://www.tejasoft.com
TejaSoft - Specialists in Code Audit, Unit Testing and Merciless Re-factoring - Engineering Crisis Turnaround Experts


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jun 14, 2008 8:58 am 
Regular
Regular

Joined: Sun Apr 13, 2008 3:04 am
Posts: 71
Location: Bangalore
Also, are aware of persistance behaviour when one tries to save child etc..

in you example if I store C, would A and B get stored/updated automatically.. my experiemnts are showing different results in different context..(needs to do some r&d)

Regards,
Nagendra

_________________
Raja Nagendra Kumar,
C.T.O
http://www.tejasoft.com
TejaSoft - Specialists in Code Audit, Unit Testing and Merciless Re-factoring - Engineering Crisis Turnaround Experts


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jun 14, 2008 9:54 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
How and where is your CascadeType configured?

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jun 14, 2008 10:35 am 
Regular
Regular

Joined: Sun Apr 13, 2008 3:04 am
Posts: 71
Location: Bangalore
every where I am using cascade = CascadeType.ALL


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.