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: Can I override primary key generation?
PostPosted: Fri Jun 13, 2008 4:05 pm 
Newbie

Joined: Thu May 15, 2008 6:43 pm
Posts: 3
Hibernate version:1.2.1 GA

I've got NHibernate generating a guid.comb for a primary key of one of my POCOs, however every so often I would prefer to provide the primary key. When I fill the property and save the new instance to the database, NHibernate generates a Guid.comb and overwrites the key I supplied.

I suppose I could save the new object, then edit it to modify it's primary key, but that's more DB use than I want in this system.

Am I missing something? Is there some way I can occasionally (1%-2% of the time) provide my own key while preserving the guid generation the rest of the time?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 16, 2008 4:03 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
You can write your own generator class, which decides e.g. on a flag on the class, if it uses the supplied key or generates a new one. Have a look at the IIdentifierGenerator interface. I think, there are also examples in the nhibernate test cases.

_________________
--Wolfgang


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.