Take the 2-minute tour ×

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)

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.

share|improve this question
add comment

3 Answers

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

share|improve this answer
add comment
up vote 0 down vote accepted

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.

share|improve this answer
Interesting concept. I'd definitely love to see it once it's working. Be sure to post back! –  ddc0660 Sep 16 '09 at 13:52
The biggest PITA so far is determining all the entity columns in advance. The linked solution relies on an "allcolumns" parameter where you specify the various seed columns you need. I'd like it to be more dynamic though. –  Sean Gough Sep 16 '09 at 18:33
add comment

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!)

share|improve this answer
For the most part I agree, not a whole lot of gain to be had and this kinda falls into the "if it ain't broke" category. To answer your question though, I think part of it is having IDs increment on a per entity basis (for situations like 1000 orders being added in between adding two customers). The other part I admit is just seeing how it can be done. :) –  Sean Gough Sep 17 '09 at 18:14
Yeah, I see what you mean regarding the order numbers. There is however a school of thought that says surrogate keys should not have any use or meaning outside of the application (i.e. the ID field should not serve as both the surrogate key and the order number). Another problem of using the ID for both purposes is if you have to rethink your database approach (when scaling for instance) and need to move to a different key strategy (but still keep the old records). –  UpTheCreek Sep 18 '09 at 8:12
Hmm, that SOT makes a lot of sense and I think those are very valid points. I'm still gonna try and get it working though, if for no other reason than getting a better idea of the inner workings. –  Sean Gough Sep 18 '09 at 14:31
add comment

Your Answer


