3

There are a few overloads:

GeneratedBy.HiLo(string maxLo);
GeneratedBy.HiLo(string maxLo, Action<ParamBuilder> paramValues);
GeneratedBy.HiLo(string table, string column, string maxLo);
GeneratedBy.HiLo(string table, string column, string maxLo, string where);
GeneratedBy.HiLo(string table, string column, string maxLo, Action<ParamBuilder> paramValues);

I understand conceptually what the hi/lo algorithm does. But what do these parameters do? I can't find the documentation in Hibernate on what they actually do.

Is maxLo supposed to be a name, or a number? What are the paramValues? What are the purposes of table and column?

Example usage based on the NHibernate getting started tutorial:

public class StoreMap : ClassMap<Store>
{
    public StoreMap()
    {
        Id(x => x.Id).GeneratedBy.HiLo("something");
        Map(x => x.Name);
        HasMany(x => x.Staff)
            .Inverse()
            .Cascade.All();
        HasManyToMany(x => x.Products)
            .Cascade.All()
            .Table("StoreProduct");
    }
}
3
  • maxLo is the low-value of the HiLo, so if you specify 10, it will generate 10 numbers to your Hi. So if your Hi is currently 3, it will generate 31 to 40 (or 30 to 39, I forget which). This only works if you allow NHibernate to generate the tables for you. If you're using Table/Column/Condition, then you can specify where NHibernate gets the Hi value. from.
    – Phill
    Aug 18, 2011 at 3:17
  • You should submit that as an answer.
    – user47589
    Aug 18, 2011 at 3:29
  • Done, I fleshed it out a little bit and included a link to my blog which shows how to configure HiLo using your own tables. Hope it helps.
    – Phill
    Aug 18, 2011 at 3:37

1 Answer 1

8

maxLo is the Lo-value of the HiLo.

Depending on the frequency of inserts into the table you should specify the value as 10, 100, or 1000. If you specify the value as 10, NHibernate will grab the Hi value from the database and use the low value to generate your HiLo.

If you insert often then you should specify a high Lo (such as 100 or 1000), if you insert less often then you should specify a low Lo. This is because a new Hi value is pulled from the database every time the session factory is created. If you have a current Hi of 5 with a Lo of 100, and have inserted 13 of 100 items, and you create a new Session Factory (from restarting the website, or app) then it will get a new Hi of 6, and you will have gaps from 513 to 600

So if your Hi is currently 3, then it will generate values from 31 to 40. Likewise if your Lo is 1000, it will generates values from 3001 to 4000.

The additional parameters allow you to specify a table where the Hi value is stored. By default NHibernate will generate this table for you if it's configured to create/update the database, however the parameters allow you to setup the tables yourself.

I wrote a blog post a while ago which shows Column per Hi, and Row per Hi.

http://www.philliphaydon.com/2010/10/24/using-hilo-with-fluentnhibernate/

Edit: Behaviour of a Lo such as 7

HiLo is calculated as a range of numbers, for example if we have a Hi of 3, and a Lo of 10, the range is between:

3*10 = 30 to 3*10+10 = 40

So the session factory will have a range of 30 to 40, when you ask for an Id for an object it grabs it's current value (30) and add's 1. So the Id = 31. The next object will take 31+1 = 32. And this occurs until it reaches 40, at which point it goes to the database and asks for the next Hi.

If you had two web servers, the second Web Server may have asked for the next Hi which was 4, which means the first Web Server will get 5.

So your next range is 50 to 60.


If the Lo was 7, we would get:

0*7 = 0 / 0*7+7 = 7 = 1-7

1*7 = 7 / 1*7+7 = 14 = 8-14

2*7 = 14 / 2*7+7 = 21 = 15-21

So it still works.

But my personal opinion is it's easier to work with whole values, 10, 100, 1000.

4
  • Added a paragraph to discuss frequency of inserts for deciding what Lo value to use.
    – Phill
    Aug 18, 2011 at 3:49
  • One more thing before I can accept: what behavior is expected if you use, say, 7, as the lo number? Does hi 1 go 1-7, and hi 2 go 8-14?
    – user47589
    Aug 19, 2011 at 0:47
  • @Inuyasha - Updated the answer, I hope that explains it for you. You should fire up a test project and have a play around so you can see it happening also. I find it always helps to have something visual.
    – Phill
    Aug 19, 2011 at 1:24
  • In NHibernate 4, the next Hi value is read from the database upon the first Save call, not when the session factory is created as might have been the case when you originally wrote the answer. This is good news for all read-only applications so that numbers are not wasted.
    – wezzix
    Feb 9, 2016 at 9:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.