Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HiLo vs Identity?

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?

like image 949
Fitzchak Yitzchaki Avatar asked Feb 28 '23 09:02

Fitzchak Yitzchaki


2 Answers

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.

like image 194
Diego Mijelshon Avatar answered Mar 08 '23 17:03

Diego Mijelshon


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

like image 43
Sheng Chien Avatar answered Mar 08 '23 15:03

Sheng Chien