DelayedPostInsertIdentifier cannot be cast to class java.lang.Integer

Description

I am in the process of migrating from Hibernate ORM 5.2 to 5.3.

Maybe we did something wrong over the last 8 or so years, but starting with Hibernate 5.3, the most basic function fails: Session.save(..) (EDIT: actually it's Session.merge() followed by a Session.save(); see comments below) now throws this:

It happens when auto-generating an ID based on table identity. The entity has this id attribute:

I could track it down to a change in behavior in AbstractSaveEventListener.performSaveOrReplicate(...):

In Hibernate ORM 5.2, requiresImmediateIdAccess was false, inTxn was true. This resulted in shouldDelayIdentityInserts to be false.

Since Hibernate ORM 5.3, shouldDelayIdentityInserts is true, because the new code also checks the FlushMode in shouldDelayIdentityInserts(...) which is always FlushMode.COMMIT in our code.

Previously, Hibernate ORM was smart enough to notice that Session.save(..) together with an autogenerated IDENTITY ID needs an immediate, non-delayed database insert. But anyway, I don't know why Hibernate even tries to put an instance of DelayedPostInsertIdentifier into an IntegerTypeDescriptor.

So, is this even a bug in Hibernate or did we do something wrong over all these years?

Environment

AdoptOpenJdk 11, MariaDB 10.2.18, MariaDB Connector/J 2.3.0

Status

Assignee

Chris Cranford

Reporter

Christian Ciach

Fix versions

Labels

None

backPortable

None

Suitable for new contributors

None

Requires Release Note

None

Pull Request

None

backportDecision

None

Worked in

5.2.17

Components

Affects versions

Priority

Major
Configure