-->
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.  [ 2 posts ] 
Author Message
 Post subject: Managing persist with child FK dependence on parent PK
PostPosted: Thu Sep 15, 2011 10:17 am 
Newbie

Joined: Thu Sep 15, 2011 9:58 am
Posts: 4
I'm new to hibernate and trying to navigate some issues. Currently I have multiple tables that contain have a foreign key pointing to the primary key of another table. The primary keys in many cases are auto generated. I'm using MS SQL Server so the database creates the generated key. I'm building the objects up and then performing entityManager.persist on the top level object. When I do this I get a constraint violation stating that null is not allowed for the foreign key value. This foreign key came from the primary key of a parent that was to be auto generated. Do I have to persist each parent, retrieve the key, set it on the child and then move on to the child? What I was hoping is that I would have the ability to build a single object and persist that one time without having to do all this individual database management. If I do have to manage it one at a time how do I retrieve it and rollback if necessary. I tried using nested transactions but it wasn't allowed?


Top
 Profile  
 
 Post subject: Re: Managing persist with child FK dependence on parent PK
PostPosted: Thu Sep 15, 2011 1:55 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Hibernate should be able to do all you need automatically. You'll need to make sure that cascading, entity relations, etc. are done correctly. The parent/child example in the Hibernate documentation should explain most of it. If you are not using hbm.xml mapping files, you may have to dig around in the rest of the documentation to find the corresponding annotations.


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