NHibernate Forge
The official new home for the NHibernate for .NET community

Comments: Creating a custom id generator for nHibernate

Wiki Page Hierarchy

Pages

Page Details

First published by:
Chris Nicola
on 09-03-2009
Last revision by:
Steve Bohlen
on 11-20-2011
1 person found this article useful.
Comments (10)
History (11)

Creating a custom id generator for nHibernate

Sort by: Published Date | Most Recent | Most Useful
1 2 Next >
By: xkevin Posted on 12-01-2009 0:05
100% of people found this useful

Thanks for your article.

However I have problem.

I would like to generate ID based on the certain field in the entity.

For example:

class Customer

{

   private string _id;

   private string _name;

}

I would like to generate id such as:

<name> + <next available id>

The <name> is the name of customer and the <next available id> get the next id automatically.

My question is: how do you pass the name of customer to id generator?

Regards,

Revin.

By: Chris Nicola Posted on 12-24-2009 21:24

Sorry I have not been checking this post, I think I need to subscribe the comments.

I'm not sure why you would want to do this exactly, it seems a bit like using business values as a primary key (you should read the nhibernate documentation about why you should always use surrogate keys).  Perhaps if you could tell me what your goal is with this strategy I might have a better solution for you.

Anyways, if you do need to do this (perhaps you are satisfying a custom database) and you want to use a custom Id generator, you could use reflection.  You would add another property to your custom id generator like "prefix = "Name"" and then you could access that from your custom id generator and use it to get the value of the "name" property during the id generator process.

I don't recommend this really, primary key's should not really change on update and technically name is a property that could be changed.

By: DIDNU Posted on 01-28-2011 11:23

This is a really intriguing idea for sure! However, after reading what Chris Nicola has said here, I think there is some point to what he has said. After all, primary keys are best left unchanged where the property name do get changed. I think the reflection method is better as it is quite easier to do and takes up pretty less time by adding another property to the id generator as when compared with this method!

www.magneticmessenger.com

By: puerta_abierta Posted on 04-11-2011 8:37

hi, I'm having a lot of preblems using nhibernate. I can not get any of the sample code fromany of the tutorials at bytesland.com/nhibernate to work. Can you please tell me where I can download the source code you are using that way I can compare what I have done to what actually works and see what is wrong. I'm also not sure if I have instally or downlaod the correct version of Nhibernate.

By: ardelialacy Posted on 06-24-2011 15:51

I'm not sure why you would want to do this exactly, it seems a bit like using business values as a primary key (you should read the nhibernate documentation about why you should always use surrogate keys).  Perhaps if you could tell me what your goal is with this strategy I might have a better solution for you.

www.bestpaintsprayers.us

Powered by Community Server (Commercial Edition), by Telligent Systems