Assertion error when running query that loads association containing transient instance

Description

An assertion error is triggered when:

  • an instance of EntityA is created an persisted

  • an instance of EntityB is created

  • another instance of EntityA is created an not persisted

  • EntityB.entityA is set to the second, non-persistence instance of EntityA

  • EntityB is persisted

  • a query is issued against EntityB in the same persistence context

For this to happen, EntitA must have no @Version attribute and a @GeneratedValue identifier: this causes Hibernate to be unable to determine whether instances of this entity are transient or simply new, so no error is thrown when persisting EntityB.

This was originally reported as a Quarkus issue, see the reproducer here: .

Activity

Show:

Gavin King last week

For this to happen, EntitA must have no @Version attribute and a @GeneratedValue identifier:

It’s also required that the user explicitly set the id to a non-default value, “tricking” Hibernate into thinking it’s detached instead of new.

If you don’t explicitly set the id, Hibernate will infer that it’s new.

Details

Assignee

Reporter

Affects versions

Priority

Created last week
Updated last week