-->
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.  [ 7 posts ] 
Author Message
 Post subject: Generated vs assigned ids
PostPosted: Tue Jan 29, 2008 5:21 pm 
Regular
Regular

Joined: Wed Aug 15, 2007 7:37 am
Posts: 73
One of my usages of NHibernate is to move objects between two different databases; for this reason I'd set the id type to be assigned (since I never want an id generated if the object doesn't exist at destination).

However, there are some cases when I do need to create new entities and I don't want to manually do the job that auto_increment does, so it would make more sense to set the id types to 'generated' for this case, but I very rarely need to use the 'generated' behaviour. I don't really want to duplicate the entire mapping file and have to maintain two copies.

The code below copies an object from database A to database B, and preserves the id x whether the object exists in B or not. If I change the id types to generated, will this remain the case?
Code:
long x = 100;
MyObject o;
{
  Session sA = GetSessionOnA();
  o = sA.Get<MyObject>(x);
}

Session sB = GetSessionOnB();
sB.SaveOrUpdateCopy(o);
sB.Flush();


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 30, 2008 4:22 pm 
Regular
Regular

Joined: Wed Aug 15, 2007 7:37 am
Posts: 73
As a followup to my own question, what I really want is an assigned ID that behaves like a native ID when nothing's been assigned (or vice versa). Looks like other people are also interested in this (http://opensource.atlassian.com/projects/hibernate/browse/HBX-1034) - I guess a custom id generator is what's required? I can't find a lot about writing and using custom generators anywhere.

Thanks,

Steve


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 31, 2008 2:54 pm 
Newbie

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

in your copier to save with an assigned ID, overriding the generator.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 31, 2008 3:20 pm 
Regular
Regular

Joined: Wed Aug 15, 2007 7:37 am
Posts: 73
This won't work with child entities and so on; it would require me not to cascade anything I think.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 01, 2008 4:43 am 
Regular
Regular

Joined: Tue Aug 08, 2006 4:28 am
Posts: 96
Location: Hong Kong
I am not sure if this will work.

Write your own generator class. The generator will check for any assigned id. If an id exists, it returns the existing id. Otherwise it calls one of the NH supplied generator of your choice.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 01, 2008 4:51 am 
Regular
Regular

Joined: Tue Aug 08, 2006 4:28 am
Posts: 96
Location: Hong Kong
Your custom generator should inherit from NHibernate.Id.IIdentifierGenerator. You will need to link to the generator in the mapping files like the following
Code:
    <id name="MyId">
      <generator class="MyNamespace.MyGenerator, MyAssembly" />
    </id>


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 01, 2008 11:40 am 
Regular
Regular

Joined: Wed Aug 15, 2007 7:37 am
Posts: 73
Yeah, that'll probably be the best solution. I'm not sure if it's that easy though, since looking at the GeneratorFactory code in the source there's some setup that goes on. I'll investigate sometime and maybe write it up somewhere, since there's very little on the web about it.


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