许多实体上的合并会在连接表中引发重复的条目

时间:2017-12-27 17:19:07

标签: java eclipselink persistence

我们有两个实体

@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;
}

和第二个实体

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

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

当我们这样做的时候     em.merge(distributionList);

eclipse链接尝试重复加入表中的条目

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 (?, ?)")
.....

任何建议?实体应该是正确的,我们合并所有者部分

1 个答案:

答案 0 :(得分:0)

可能是你的List-s包含重复项。尝试将列表类型更改为“设置”。