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

java eclipselink persistence

78 观看

1回复

38 作者的声誉

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

作者: user2946593 的来源 发布者: 2017 年 12 月 27 日

回应 1


0

828 作者的声誉

决定

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

作者: Anton Tupy 发布者: 2017 年 12 月 27 日
32x32