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

@ManyToMany and @JoinTable adds duplicated records in the database.

Description

The following setup:

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 @Entity public class Parent { @Id @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seq") private int id; @ManyToMany(fetch = FetchType.LAZY, targetEntity = Child.class, cascade = CascadeType.MERGE) @JoinTable(name = "parent_child", joinColumns = @JoinColumn(name = "parent_id"), inverseJoinColumns = @JoinColumn(name = "child_id")) private List<Child> children; public int getId() { return id; } public void setId(int id) { this.id = id; } public List<Child> getChildren() { return children; } public void setChildren(List<Child> children) { this.children = children; } }
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 @Entity public class Child { @Id @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seq") private int id; @ManyToMany(fetch = FetchType.LAZY, targetEntity = Parent.class, cascade = CascadeType.MERGE) @JoinTable(name = "parent_child", joinColumns = @JoinColumn(name = "child_id"), inverseJoinColumns = @JoinColumn(name = "parent_id")) private List<Parent> parents; public int getId() { return id; } public void setId(int id) { this.id = id; } public List<Parent> getParents() { return parents; } public void setParents(List<Parent> parents) { this.parents = parents; } }

and this test-case:

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 @Test public void hhh123Test() throws Exception { // persist parent entity in a transaction EntityManager em = entityManagerFactory.createEntityManager(); em.getTransaction().begin(); Parent parent = new Parent(); em.persist(parent); //em.flush(); int id = parent.getId(); em.getTransaction().commit(); em.close(); // relate and persist child entity in a new transaction em = entityManagerFactory.createEntityManager(); em.getTransaction().begin(); parent = em.find(Parent.class, id); // *: parent.getChildren().size(); assertEquals(0, parent.getChildren().size()); Child child = new Child(); child.setParents(Arrays.asList(parent)); parent.setChildren(Arrays.asList(child)); em.persist(child); em.persist(parent); //em.flush(); assertEquals(1, parent.getChildren().size()); // -> [Child@36bf7916, Child@36bf7916] em.getTransaction().commit(); em.close(); em = entityManagerFactory.createEntityManager(); em.getTransaction().begin(); Parent p = em.find(Parent.class, id); // FAILURE: 1 expected, but 2 childs in List Assert.assertEquals(1, p.getChildren().size()); em.getTransaction().commit(); em.close(); }

Here's the repository with reproduceable test-case: https://github.com/ptahchiev/hibernate-test-case-templates/tree/HHH-6776

Environment

None

Status

Assignee

Unassigned

Reporter

Petar Tahchiev

Fix versions

None

Labels

None

backPortable

None

Suitable for new contributors

None

Requires Release Note

None

Pull Request

None

backportDecision

None

Affects versions

5.2.17

Priority

Major