We're updating the issue view to help you get more done. 

ManyToMany List update with detached entities throws EntityExistsException

Description

I know this is not a nice usage of Hibernate in general but we came across this issue during our Spring Boot migration (5.0.12 to 5.2.17)

Given an entity with ManyToMany list (not sure about ManyToOne ) updating the list with the same detached entities would cause an EntityExistsException.

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 @Entity @Audited public static class Alert { @ManyToMany private List<RuleName> ruleNames = new ArrayList<>(); } @Entity @Audited public static class RuleName { } @Test @Priority(7) public void testClearAddDetachedOutsideTransaction() { EntityManager em = getEntityManager(); em.getTransaction().begin(); List<RuleName> clone = Arrays.asList(em.find(RuleName.class, ruleName1Id), em.find(RuleName.class, ruleName2Id)); em.getTransaction().rollback(); em.getTransaction().begin(); Alert alert = em.find(Alert.class, alertId); alert.getRuleNames().clear(); alert.getRuleNames().addAll(clone); em.persist(alert); em.getTransaction().commit(); //Exception thrown }

Environment

5.2.17, Java8

Status

Assignee

Chris Cranford

Reporter

Bahri Gencsoy

Fix versions

Labels

None

backPortable

None

Suitable for new contributors

None

Requires Release Note

None

Pull Request

None

backportDecision

None

Worked in

5.0.12

Components

Affects versions

5.2.17

Priority

Minor