Uninitialized ManyToMany Entry is not removed when bytecode-enhanced Entity is deleted

Description

We noticed that em.remove() of bytecode-enhanced Entity1 with an unitialized ManyToMany collection of Entity2 fails because of the database FK constraint on table Entity1_Entity2. When examining the problem, we found that the entry in the table Entity1_Entity2 was not deleted before deleting Entity1. Please find attached a test case that demonstrates the problem, here a short excerpt:

@Entity
public class Entity1 {
@Id

private Long id;

@ManyToMany

private List<Entity2> entities2 = new ArrayList<>();
...
}

In the test case, we do:

entity1.getEntities2().add(entity2);
...
em.remove(entity1);

which fails.

The test case starts to work if either:

  • the ManyToMany collection is initialized by calling size() before em.remove()

  • the bytecode-enhancement is switched off

  • we go back to hibernate 4.3.x

Environment

JPA, Java 8, Oracle/H2

Status

Assignee

Luis Barreiro

Reporter

Jörg Mattes

Fix versions

backPortable

None

Suitable for new contributors

None

Requires Release Note

None

Pull Request

None

backportDecision

None

Affects versions

Priority

Major
Configure