-->
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.  [ 8 posts ] 
Author Message
 Post subject: Hibernate doesn't seem to be inserting entities in order
PostPosted: Tue Oct 21, 2003 5:14 am 
Beginner
Beginner

Joined: Mon Sep 08, 2003 6:52 am
Posts: 46
Hi there.

I have a tree of hibernate objects - it looks something like

A -> multiple B's -> multiple C's -> Multiple D's

There are times when I create an A, add a B to it, add some C's to B, and then add a D to each C.
Then I do session.save(A). This works fine in MySQL, but trying to do it on PostgreSQL causes a foreign key constraint, because it tries to do the insert the D's before it's inserted the A.

Is there any way to enforce the insertion order, or am I missing something, or...?

Thanks,

Hunter


Top
 Profile  
 
 Post subject: Foreign Key Constraint
PostPosted: Wed Oct 22, 2003 8:17 pm 
Regular
Regular

Joined: Tue Sep 09, 2003 9:37 pm
Posts: 56
Location: Ogden, Utah, USA
Are you sure that's what's happening? I've been suspicious of that several times but it's never been Hibernate's fault. :)

Typically it doesn't matter what order you save the records in as long as you don't have not-null constraints on your foreign keys. You may want to set show_sql=true and make sure you understand what is being inserted.

Good luck!
Jenica


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 23, 2003 12:53 pm 
Beginner
Beginner

Joined: Mon Sep 08, 2003 6:52 am
Posts: 46
Well I do have not-null constraints on the keys. It's perfectly reasonable to do that, and still expect Hibernate to do the insertions in the correct order.

Can't it just start at the top and work it's way downwards? I actually only save the top object, so it's doing the inserts for the cascades, but for some reason is doing the children first, which, in the case of constrained foreign keys, is incorrect.

And the more I think about it, it's also more inefficient to insert the child, insert the parent, and then update the child with the parent key, when it could insert the parent, and then the child...

H


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 23, 2003 1:45 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Use cascade="save-update"


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 24, 2003 4:53 am 
Beginner
Beginner

Joined: Mon Sep 08, 2003 6:52 am
Posts: 46
I have cascade="all-delete-orphan".

It's doing the cascade, it's just doing it in the wrong order.

H


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 24, 2003 4:59 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
So, find your bug then.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 03, 2005 7:40 pm 
Newbie

Joined: Tue Nov 01, 2005 9:55 pm
Posts: 14
I am having a similar problem:

my mapping between objects is as follows:

C1 -> C2
^ ^
B1 -> B2
^ ^
A1 -> A2

all haev cascade all constraint, the order i would expect them is:
A1, A2, B1, B2, C1, C2 or
A1, B1, C1, A2, B2, C2

but the order i get is:
A1, B1, C1, C2, A2, B2

so it seems that it doesn evaluate the dependencies correctly when there are more than one parent, is there any way to force it to iterate in the right order?

thanks,
tomer


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 04, 2005 11:41 am 
Newbie

Joined: Tue Nov 01, 2005 9:55 pm
Posts: 14
I also tried specifying not-null on all parents adn got this exception:

org.hibernate.PropertyValueException: not-null property references a null or transient value

so it doesnt affects the order of saving the entities


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