# Hibernate 6, id generator table strategy start value issue

**URL:** <https://discourse.hibernate.org/t/hibernate-6-id-generator-table-strategy-start-value-issue/7655>\
**Category:** Hibernate ORM\
**Created:** [May 18, 2023, 1:20pm UTC](https://discourse.hibernate.org/t/hibernate-6-id-generator-table-strategy-start-value-issue/7655 "2023-05-18T13:20:36Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![amir](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/amir/32/2415_2.png) [@amir](https://discourse.hibernate.org/u/amir)\
**Post date:** [May 18, 2023, 1:20pm UTC](https://discourse.hibernate.org/t/hibernate-6-id-generator-table-strategy-start-value-issue/7655/1 "2023-05-18T13:20:36Z")

</div>

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

```auto
@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://github.com/hibernate/hibernate-test-case-templates/pull/267>
>
> \- Test for table strategy id generator, demonstrates that it returns too low id

---

<div class="post-metadata">

**Author:** ![amir](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/amir/32/2415_2.png) [@amir](https://discourse.hibernate.org/u/amir)\
**Post date:** [May 18, 2023, 1:29pm UTC](https://discourse.hibernate.org/t/hibernate-6-id-generator-table-strategy-start-value-issue/7655/2 "2023-05-18T13:29:12Z")

</div>

[https://hibernate.atlassian.net/browse/HHH-16634](https://hibernate.atlassian.net/browse/HHH-16634)

---

<div class="post-metadata">

**Author:** ![beikov](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/beikov/32/258_2.png) [@beikov](https://discourse.hibernate.org/u/beikov)\
**Post date:** [May 22, 2023, 7:54am UTC](https://discourse.hibernate.org/t/hibernate-6-id-generator-table-strategy-start-value-issue/7655/3 "2023-05-22T07:54:21Z")

</div>

See the migration guide about what to do here: [hibernate-orm/migration-guide.adoc at 6.0 · hibernate/hibernate-orm · GitHub](https://github.com/hibernate/hibernate-orm/blob/6.0/migration-guide.adoc#defaults-for-implicit-sequence-generators)  
You have to update the sequence to a multiple of `allocationSize` + 1, as that is what Hibernate does internally.

---

<div class="post-metadata">

**Author:** ![luiscla27](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/luiscla27/32/3309_2.png) [@luiscla27](https://discourse.hibernate.org/u/luiscla27)\
**Post date:** [June 25, 2024, 10:00pm UTC](https://discourse.hibernate.org/t/hibernate-6-id-generator-table-strategy-start-value-issue/7655/4 "2024-06-25T22:00:47Z")

</div>

@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??

---

<div class="post-metadata">

**Author:** ![beikov](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/beikov/32/258_2.png) [@beikov](https://discourse.hibernate.org/u/beikov)\
**Post date:** [July 4, 2024, 1:09pm UTC](https://discourse.hibernate.org/t/hibernate-6-id-generator-table-strategy-start-value-issue/7655/5 "2024-07-04T13:09:47Z")

</div>

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`
