Search code examples
javaeclipselinkpersistence

merge on a manyto many entity provoke a duplicate entry in jointable


We have two entity

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "")
@Entity
@Table(name = "distributionlists", schema = "")
public class DistributionList implements Serializable
{
....
 @ManyToMany(targetEntity = Contact.class)
@JoinTable(
        name = "distributionlists_contacts",
        joinColumns = @JoinColumn(name = "dl_id", referencedColumnName = "dl_id"),
        inverseJoinColumns = @JoinColumn(name = "con_id", referencedColumnName = "con_id"))
private List<Contact> contacts;
}

and second entity

 @XmlRootElement
 @XmlType(name = "")
 @Entity
 @Table(name = "contacts", schema = "")
public class Contact implements Serializable
{
 ....

@ManyToMany(targetEntity = DistributionList.class, mappedBy = "contacts")
private List<DistributionList> distributionLists;
}

when we do em.merge(distributionList);

eclipse link try to duplicate entry in jointable

Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.6.3.qualifier): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '1-1' for key 'PRIMARY'
Error Code: 1062
Call: INSERT INTO distributionlists_contacts (con_id, dl_id) VALUES (?, ?)
bind => [2 parameters bound]
Query: DataModifyQuery(name="contacts" sql="INSERT INTO distributionlists_contacts (con_id, dl_id) VALUES (?, ?)")
.....

any suggest? The entity should be correct, and we merge the owner part


Solution

  • May be your List-s contains duplicates. Try to change type of collections from List to Set.