HiLo best practice

437 views
Skip to first unread message

Gareth

unread,
Sep 1, 2009, 10:47:54 AM9/1/09
to nhusers
Hi all,

Is it considered a good practice to create a new hilo column for each
entity i have, eg given two entities Person and Order should my
hibernate_unique_key table contain two rows 'nextPersonHi', and
'nextOrderHi'? In my mind this would give a larger range of id's
available to the system - although I may be completly wrong and have
misunderstood the HiLo generator!

Thanks

Tuna Toksoz

unread,
Sep 1, 2009, 11:28:55 AM9/1/09
to nhu...@googlegroups.com
Hilo range is already _big_
--

Tuna Toksöz
Eternal sunshine of the open source mind.

http://devlicio.us/blogs/tuna_toksoz
http://tunatoksoz.com
http://twitter.com/tehlike

Fabio Maulo

unread,
Sep 1, 2009, 11:37:48 AM9/1/09
to nhu...@googlegroups.com
In this case the matter is not "how large is the range of ids" but "how small should be the ids fragmentation in a table"

2009/9/1 Gareth <gee...@hotmail.co.uk>



--
Fabio Maulo

Gareth

unread,
Sep 1, 2009, 11:39:20 AM9/1/09
to nhusers
Ok - what about the good practice, is it a good idea to have a column
per entity?

On Sep 1, 4:28 pm, Tuna Toksoz <tehl...@gmail.com> wrote:
> Hilo range is already _big_
>

Fabio Maulo

unread,
Sep 1, 2009, 11:44:34 AM9/1/09
to nhu...@googlegroups.com
a row per entity... have a look to the "where" parameter of the HighLow generator.

2009/9/1 Gareth <gee...@hotmail.co.uk>



--
Fabio Maulo

Gareth

unread,
Sep 2, 2009, 5:50:34 AM9/2/09
to nhusers
I've implemented this and my mappings look like the following

<class name="User" table="userTable">
<id name="Id" column="id" unsaved-value="0">
<generator class="hilo">
<param name="table">hi_value</param>
<param name="column">userNextHi</param>
</generator>
</id>
...
</class>

<class name="Order" table="userOrder">
<id name="Id" column="id" unsaved-value="0">
<generator class="hilo">
<param name="table">hi_value</param>
<param name="column">orderNextHi</param>
</generator>
</id>
...
</class>

Whilst this works fine the SchemaExport does not add both the columns,
only the last one (orderNextHi). Is there a way to make the schema
export pay attention to the extra columns?

Thank you for your help




On Sep 1, 4:44 pm, Fabio Maulo <fabioma...@gmail.com> wrote:
> a row per entity... have a look to the "where" parameter of the HighLow
> generator.
>
> 2009/9/1 Gareth <gee-...@hotmail.co.uk>
>
>
>
>
>
> > Ok - what about the good practice, is it a good idea to have a column
> > per entity?
>
> > On Sep 1, 4:28 pm, Tuna Toksoz <tehl...@gmail.com> wrote:
> > > Hilo range is already _big_
>
> > > On 9/1/09, Gareth <gee-...@hotmail.co.uk> wrote:
>
> > > > Hi all,
>
> > > > Is it considered a good practice to create a new hilo column for each
> > > > entity i have, eg given two entities Person and Order should my
> > > > hibernate_unique_key table contain two rows 'nextPersonHi', and
> > > > 'nextOrderHi'?  In my mind this would give a larger range of id's
> > > > available to the system - although I may be completly wrong and have
> > > > misunderstood the HiLo generator!
>
> > > > Thanks
>
> > > --
>
> > > Tuna Toksöz
> > > Eternal sunshine of the open source mind.
>
> >http://devlicio.us/blogs/tuna_toksozhttp://tunatoksoz.comhttp://twitt...
>
> --
> Fabio Maulo

Oskar Berggren

unread,
Sep 2, 2009, 6:55:57 AM9/2/09
to nhu...@googlegroups.com
Have you considered the "where" clause as Fabio suggested, with one
row per value instead of column-per-value?

From the docs:

hilo

uses a hi/lo algorithm to efficiently generate identifiers of any
integral type, given a table and column (by default
hibernate_unique_key and next_hi respectively) as a source of hi
values. The hi/lo algorithm generates identifiers that are unique only
for a particular database. Do not use this generator with a
user-supplied connection.

You can use the "where" parameter to specify the row to use in a
table. This is useful if you want to use a single tabel for your
identifiers, with different rows for each table.


/Oskar


2009/9/2 Gareth <gee...@hotmail.co.uk>:

Paulo Quicoli

unread,
Sep 2, 2009, 7:53:56 AM9/2/09
to nhu...@googlegroups.com
i searched into docs, about "where" clause in hi/lo,
but i didnt find it

2009/9/2 Oskar Berggren <oskar.b...@gmail.com>:
--
Paulo R. Quicoli

Editor Técnico - ClubeDelphi Magazine - DevMedia

Gareth

unread,
Sep 2, 2009, 8:00:53 AM9/2/09
to nhusers
Im not sure on how to do this - do you have an example anywhere. If I
do the following in the respective classes it builds but saving
entities fails as it looks like the table has been locked and the
updates to increase the next hi by one keeps failing, plus the filter
column does not appear in the SchemaExport

<generator class="hilo">
<param name="where">filter=1</param>
</generator>
<generator class="hilo">
<param name="where">filter=2</param>
</generator>

and if i try something like below it won't compile.

<generator class="hilo">
<param name="column" where="filter=1">next_hi</param>
</generator>

Thank you for you time with this - Im sorry I don't yet understand.

On Sep 2, 11:55 am, Oskar Berggren <oskar.bergg...@gmail.com> wrote:
> Have you considered the "where" clause as Fabio suggested, with one
> row per value instead of column-per-value?
>
> From the docs:
>
> hilo
>
>     uses a hi/lo algorithm to efficiently generate identifiers of any
> integral type, given a table and column (by default
> hibernate_unique_key and next_hi respectively) as a source of hi
> values. The hi/lo algorithm generates identifiers that are unique only
> for a particular database. Do not use this generator with a
> user-supplied connection.
>
>     You can use the "where" parameter to specify the row to use in a
> table. This is useful if you want to use a single tabel for your
> identifiers, with different rows for each table.
>
> /Oskar
>
> 2009/9/2 Gareth <gee-...@hotmail.co.uk>:
> >> Fabio Maulo- Hide quoted text -
>
> - Show quoted text -

Fabio Maulo

unread,
Sep 2, 2009, 8:44:40 AM9/2/09
to nhu...@googlegroups.com
2009/9/2 Paulo Quicoli <pauloq...@gmail.com>


i searched into docs, about "where" clause in hi/lo,
but i didnt find it

--
Fabio Maulo

Gareth

unread,
Sep 2, 2009, 8:52:37 AM9/2/09
to nhusers
I've read that but it's not clear to me where the "where" clause
should go as neither of my attempts seemed to work. Im using
NHibernate 2.0 so maybe this where clause wasn't available in 2.0?

On Sep 2, 1:44 pm, Fabio Maulo <fabioma...@gmail.com> wrote:
> 2009/9/2 Paulo Quicoli <pauloquic...@gmail.com>

Fabio Maulo

unread,
Sep 2, 2009, 9:15:00 AM9/2/09
to nhu...@googlegroups.com
is there since nh1.1

2009/9/2 Gareth <gee...@hotmail.co.uk>



--
Fabio Maulo

Chris Nicola

unread,
Sep 2, 2009, 11:27:59 AM9/2/09
to nhu...@googlegroups.com
Something to consider, if you are using more than one column for hilo you will not be able to use SchemaExport as it only creates the first column it sees in mappings that use a TableGenerator (which hilo derives from).  If you don't plan to use SchemaExport to create your database, or do testing, then there is no issues here.

It is possible to work around this though as I mentioned here: http://www.lucisferre.org/coding/index.php/2009/08/15/generate-custom-ddl-for-your-custom-id-generator-with-nhibernate.

Chris

Fabio Maulo

unread,
Sep 2, 2009, 12:12:00 PM9/2/09
to nhu...@googlegroups.com
Would be nice to have that post as part of the How-To wiki in www.nhforge.org


2009/9/2 Chris Nicola <chni...@gmail.com>

Something to consider, if you are using more than one column for hilo you will not be able to use SchemaExport as it only creates the first column it sees in mappings that use a TableGenerator (which hilo derives from).  If you don't plan to use SchemaExport to create your database, or do testing, then there is no issues here.

It is possible to work around this though as I mentioned here: http://www.lucisferre.org/coding/index.php/2009/08/15/generate-custom-ddl-for-your-custom-id-generator-with-nhibernate.

Chris







--
Fabio Maulo

Chris Nicola

unread,
Sep 3, 2009, 12:12:56 PM9/3/09
to nhu...@googlegroups.com
Of course, can I add it to the how-to wiki?

Chris Nicola

unread,
Sep 3, 2009, 12:43:20 PM9/3/09
to nhu...@googlegroups.com

Fabio Maulo

unread,
Sep 3, 2009, 1:17:29 PM9/3/09
to nhu...@googlegroups.com
Thanks to share in a good place for the whole community.

2009/9/3 Chris Nicola <chni...@gmail.com>



--
Fabio Maulo
Reply all
Reply to author
Forward
0 new messages