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:
public class ExplicitOrAutoGenerator extends IdentityGenerator {
@Override
public Serializable generate(SharedSessionContractImplementor s, Object obj) {
if (obj instanceof ExplicitId entity && entity.getExplicitId() != null) {
return entity.getExplicitId();
}
return IdentifierGeneratorHelper.POST_INSERT_INDICATOR;
}
}
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).