-->
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.  [ 4 posts ] 
Author Message
 Post subject: Manually set primary key
PostPosted: Tue Jan 22, 2008 2:31 pm 
Newbie

Joined: Mon Oct 31, 2005 10:04 pm
Posts: 5
For a part of my project, I need to load model data from XML files and save them out to the database (much like the test fixtures in Ruby on Rails). To properly create the object graphs, I have to manually specify the primary keys like this:

<Parent Id="1" ... />
<Child ParentId = "1" ... />

My models use sequences for the primary keys normally, so manually setting the primary key has no effect. Any ideas how I could bypass the key generation in this one instance?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 22, 2008 2:50 pm 
Expert
Expert

Joined: Mon Nov 26, 2007 2:29 pm
Posts: 443
hansenm,

Set the "generator" element of the "id" entry to "assigned"
(Reference documentation, 5.1.4.1. Generator).

_________________
Gonzalo Díaz


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 22, 2008 3:04 pm 
Newbie

Joined: Fri May 18, 2007 1:41 pm
Posts: 8
use
Code:
session.Save( obj, id )

To save it with an assigned ID when the ID is normally auto generated.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 22, 2008 4:27 pm 
Newbie

Joined: Mon Oct 31, 2005 10:04 pm
Posts: 5
@gonzao_diaz: My goal was to do this without having to have separate hbm files for each situation. Everywhere else, my program uses the "native" generator.

@mkb137: Excellent! This worked great perfectly. I had tried the SaveOrUpdateCopy method without success, but I didn't see the overload of Save. Thanks a lot.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.