-->
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.  [ 3 posts ] 
Author Message
 Post subject: Cascade saving problems
PostPosted: Thu Sep 27, 2007 7:25 am 
Beginner
Beginner

Joined: Wed Nov 08, 2006 8:24 am
Posts: 26
Location: Gothenburg, Sweden
I have a structure with several tables in association with each other. It basically looks like this: TableA has an on-to-many relationship to TableB, TableB has an one-to-many relationship to TableC, and so forth for five tables... TableB has a foreign key constraint in regard to TableA, and TableC has a foreign key to TableB.

In my C#-code I start with creating a TableAMapping object, setting the id to 0 (which is the unsaved-value in my Hibernate mapping declaration). Then I go on and create a list of TableBMapping objects, setting their id’s to 0, and the property TableBMapping.TableA property to the newly created TableAMapping object (the foreign key constraint). The same goes for the other object. In the end I assign the TableBMapping list to the TableAMapping.TableB property.

Nothing weird, although it’s hard to explain in text and not in code. The problem arises when I try to save the TableAMapping object to the database. I get a foreign key exception from the database, saying that the TableB.TableA property is faulty. And of cause it is faulty; it is not set since I don’t know the TableA’s id until I save the object to the database.

In my world, shouldn’t Hibernate set TableA’s id automatically? How do I solve this problem? Do I need to save each object on its own?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 27, 2007 8:29 am 
Beginner
Beginner

Joined: Fri May 18, 2007 10:28 am
Posts: 48
Location: Madison, WI
Can you post your hbm file. I think you are not setting the cascade flag.

_________________
Please rate if it helped


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 27, 2007 4:42 pm 
Expert
Expert

Joined: Fri Oct 28, 2005 5:38 pm
Posts: 390
Location: Cedarburg, WI
There is a known bug where if you have entity A with collection property Bs, and you create a transient A, transient B, associate them via A.Bs.Add(B) and B.A = A, then try to save either one, you will get an exception about it referencing another transient entity.

Until the bug is fixed, you have to save A before associating it with B.


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