orphanRemoval not working when other same-table-relations are initialized

Description

using these entities:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 @Entity @Table(name = "ITEM", indexes = @Index(columnList = "CODE", unique = true)) public class Item implements Serializable { private static final long serialVersionUID = 1L; @Id @GeneratedValue protected Long id; @Column protected String code; @OneToMany(mappedBy = "parent", cascade = CascadeType.ALL, orphanRemoval = true) protected Set<ItemRelation> lowerItemRelations = new LinkedHashSet<>(); @OneToMany(mappedBy = "child", cascade = CascadeType.ALL, orphanRemoval = true) protected Set<ItemRelation> higherItemRelations = new LinkedHashSet<>(); ... } @Entity @Table(name = "ITEM_RELATION", indexes = @Index(columnList = "PARENT_ID, CHILD_ID", unique = true)) public class ItemRelation implements Serializable { private static final long serialVersionUID = 1L; @Id @GeneratedValue protected Long id; @ManyToOne(optional = false) @JoinColumn(name = "PARENT_ID") private Item parent; @ManyToOne(optional = false) @JoinColumn(name = "CHILD_ID") private Item child; @Column(nullable = false, columnDefinition = "INT DEFAULT 0 NOT NULL") private int quantity = 1; ... }

when executing:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 EntityManager em = emf.createEntityManager(); EntityTransaction tx = em.getTransaction(); tx.begin(); Item item = em.createQuery("select x from Item x where x.code = 'first'", Item.class).getSingleResult(); Set<ItemRelation> lowerItemRelations = item.getLowerItemRelations(); Hibernate.initialize(lowerItemRelations); // initializing higherItemRelations prevents orphanRemoval to work on lowerItemRelations Set<ItemRelation> higherItemRelations = item.getHigherItemRelations(); Hibernate.initialize(higherItemRelations); lowerItemRelations.clear(); tx.commit(); em.close();

no DELETE statement is issued.

If you don't initialize higherItemRelations then everything is working fine.

I'll produce a unit test as soon as I can.

Thanks

Environment

tested with MySQL 5.7 and embedded Derby

Status

Assignee

Unassigned

Reporter

Michele Mariotti

Fix versions

backPortable

None

Suitable for new contributors

None

Requires Release Note

None

Pull Request

None

backportDecision

None

Components

Affects versions

4.3.11
5.0.11
5.2.2
4.2.21

Priority

Major