Tutorial :Fluent NHibernate — class/entity specific next_hi seeds when using HiLo generator


Basically I'm trying to do the same thing as this question but with Fluent NHibernate.

Here is my id generation convention:

    public class IdGenerationConvention : IIdConvention      {              public void Apply(IIdentityInstance instance)              {                      instance.GeneratedBy.HiLo("1000");              }      }  

Now this works great, but all classes end up using the same next_hi.

    create table hibernate_unique_key (              next_hi INTEGER       )  

Does anyone know how to specify that each class should use it's own next_hi?

To clarify, I'd like to end up with something like customer_next_hi and order_next_hi, assuming it works based on columns. If it's row based then that's fine too, provided each entity knows which row to use for it's next_hi value.


There's a nice solution to this problem posted on Anthony Dewhirt's blog over here: http://www.anthonydewhirst.blogspot.co.uk/2012/02/fluent-nhibernate-solution-to-enable.html


I also asked this question in the Fluent NHibernate Google Group and it appears that the only way to do this would be to create a custom id generator class that either inherits from HiLo or TableGenerator. From what I gather, the key issue is that the SQL statement to create the seed table is only executed once, and there isn't a built-in override or hook to get it to create entity specific columns or rows.

So something along the lines of this is what I'll need to do. I'll post more once I have a working implementation.


Interesting question, but I wonder why you want to do it? On the (fairly minor) downside you would be generating more db requests. On the upside you would be increasing your surrogate ID space by some factor. But isn't Long/BigInt enough for them all combined? (max 18,446,744,073,709,551,615 ids!)

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »