Hibernate 6.2 broke hybrid before/after execution ID generators

Description

In Hibernate 6.2, it seems my ID generator can no longer work.

My generator uses an identity column/post-insert-id by default, but will use an explicitly supplied ID if entity.getExplicitId() returns non-null. This is handy when data is in the process of being migrated and the service can both insert new IDs as well as consume inserts (using supplied IDs) from another source.

The problem is that in 6.2, the way to choose between generation before execution (supplied ID) and generation after execution (post-insert) is via org.hibernate.generator.Generator.generatedOnExecution(). This method does not take the entity object as an argument, so the generator cannot decide at runtime which strategy to use. Is there another way to do it?

For reference, here is my generator implementation which was working in previous Hibernate versions:

If I use it as-is (by implementing the new IdentifierGenerator interface), an insert with a post-insert ID just fails because it tries to call the entity ID setter with POST_INSERT_INDICATOR (which is not a Long or Integer obviously).

Activity

Show:

Gavin King January 22, 2025 at 11:26 AM

Dupe.

Gavin King January 22, 2025 at 11:25 AM

This was already fixed in 6.4 by as part of .

이희구 August 19, 2024 at 6:25 AM
Edited

Additionally, I think it is difficult to create a composite generator class using classes that implement the Generator interface.

However, hibernate official doc is not easy. identifier section is very lacking in content. . In particular, the explanation of the IdGeneratorType annotation is extremely crude. It only says that three parameters must be specified in the constructor. Moreover, one of them is an entity class, and the other two classes also create default pre-built generators internally and do not help in calling methods. Rather, the deprecated GenericGenerator makes it easier to see what is needed at a glance.

이희구 August 19, 2024 at 5:00 AM

I am using hibernate 6.5.2 and was able to respond to the above issue by writing the code as follows.

With this code, I succeeded in inserting the existing value from the user_backup table into the user table without increasing the ID.

But allowAssignedIdentifiers method had no effect on execution regardless of its value. This is weired point.

The code used in version 5 is as follows.

Hinrik May 7, 2024 at 2:22 PM
Edited

Given the release notes of 6.5.0, I hoped this would work:

But when I try it on an entity, it always gets an auto-generated ID from MySQL instead of the one I manually assigned.

Fixed

Details

Assignee

Reporter

Fix versions

Affects versions

Priority

Created May 25, 2023 at 9:06 AM
Updated January 22, 2025 at 11:26 AM
Resolved January 22, 2025 at 11:26 AM