Hibernate SequenceHiLoGenerator

Recently I digged a little in the mechanism of our ID generation.
One of our IDs was annotated like this:
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "myIdGen")
@SequenceGenerator(name = "myIdGen", sequenceName = "MY_SEQUENCE")
protected Long id;

But seemingly the underlying (Oracle-)sequence remained untouched.
On an empty DB, with a sequence value of 1, the id that was returned by:
Serializable id = this.getHibernateTemplate().save(domainObject);
was 50. Strange! I would have expected: 1.

The Logfiles then hinted me, that Hibernate chose the SequenceHiLoGenerator for ID Generation.

Looking through the web here, here and here , I came  close to understanding the algorithm.

The idea is to not touch the DB everytime an ID is needed but to „reserve“ a block of numbers, using 50 as a default block size. Reservation is done by: 50*CurrentSequenceValue. Then Hibernate adds 1 to this base value everytime a new id is needed. When it has added 50 times, a new block is reserved using a fresh sequence value.

Perhaps this picture makes it clearer:

All of this configurable of course. I am a little surprised that this is the Hibernate default though.

 

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert