'Detached entity passed to persist' during merge when enableLazyInitialization is set to true

Description

After migrating hibernate from 4.3.11 to 5.1.10 and using the new hibernate-enhance-maven-plugin replacing the deprecated ant bytecode enhancement task, the following happens with an entity with OneToMany association and CascadeType.ALL.

  • enableLazyInitialization set to false and the test passes

  • enableLazyInitialization set to true and the test fails with "Detached entity passed to persist"

App worked fine with 4.3.11 version of hibernate and the old bytecode enhancement with ant task.

I can work around this by removing CascadeType.PERSIST but needs to modify lot of associations and the related service classes for persisting new entity.

From the debugging, I found that a Persist event triggers followed by Merge event when CascadeType.ALL is set on an association and the entities are detached when enableLazyInitialization is set to true.

Please find the test attached.

Environment

Hibernate 5.2.12 and 5.1.10, Oracle 12c, H2 1.3.176, Oracle jdk1.8.0_121

Status

Assignee

Luis Barreiro

Reporter

Goutham D

Fix versions

Labels

backPortable

None

Suitable for new contributors

None

Requires Release Note

None

Pull Request

None

backportDecision

None

Affects versions

5.2.12
5.1.10

Priority

Major