4

This is the same question as: HiLo or identity?

Let's take the database of this site as an example.

Lets say that the site has the following tables:

  • Posts.
  • Votes.
  • Comments.

What is the best strategy to use for it:

Identity - which is more common.

OR

HiLo - which give best performance.

Edit: if HiLo is the best, how the structure of the DB would be?

Community
  • 1
  • 1
Fitzchak Yitzchaki
  • 9,095
  • 12
  • 56
  • 96
  • I'm a fan of sequential ids. HiLo is better suited to applications that subscribe to the smart client pattern where the application starts online gets it's HiLo key and then can function offline while generating unique keys for the application. – Chris Marisic Mar 15 '10 at 19:39
  • 1
    HiLo is good to any case that you need the ID, of the new added entity, Before you commit the transaction. – Fitzchak Yitzchaki Mar 15 '10 at 21:38

2 Answers2

7

Not only HiLo gives better perfomance pretty much always, but it also preserves the Unit of Work by avoiding ALL writes until session flush.

Being "more common" (?) doesn't seem like an objective technical advantage.

I recommend that you read http://fabiomaulo.blogspot.com/2009/02/nh210-generators-behavior-explained.html for a detailed explanation of the downsides of choosing identity.

Diego Mijelshon
  • 52,548
  • 16
  • 116
  • 154
1

I have some sample codes on how to use MultipleHiLoPerTableGenerator with Hibernate annotation.

Please refer to the bottom part of this blog:

http://shengchien.blogspot.com/2010/10/hibernate-batch-processing-with-mysql.html

Sheng Chien
  • 519
  • 4
  • 7