Hibernate 6, id generator table strategy start value issue

Tried the following test case in hibernate 6.2.2:
Created entity with the following setup for id

@GeneratedValue(generator= "entityD", strategy = GenerationType.TABLE)
    @TableGenerator(name = "entityD", table="my_seq",pkColumnName = "my_seq_name", valueColumnName = "my_seq_val")

after that update generator for corresponding entity to 50 in the my_seq table via sql.
and ask hibernate to insert new entity.
The id will be assigned is 2, while I would expect 51…(or 52, or >50 I don’t care)
if I update generator to 100, the assigned id will be 52…

Seems a bug? In prev versions(3-5) it was working as expected.

Looking briefly in the code I see hibernate reads value from the table, updates with increment and then returns the value that was read, and after that applies formula value read - (increment size-1).
Seems logic should either return value after update or don’t substract increment size, which is 50 by default…

Test that demonstrates issue

https://hibernate.atlassian.net/browse/HHH-16634

See the migration guide about what to do here: hibernate-orm/migration-guide.adoc at 6.0 · hibernate/hibernate-orm · GitHub
You have to update the sequence to a multiple of allocationSize + 1, as that is what Hibernate does internally.

@beikov, I couldn’t find any explicit references of the HiLo algorithm int the migration guide.

Does the new implementation accounts for the HiLo algorithm use cases??

HiLo is the default optimizer. The change that happened in ORM 6 is that now a sequence per entity is created/expected by default instead of a global sequence. You should be able to revert to the old behavior if you want by setting hibernate.id.db_structure_naming_strategy=legacy

1 Like