2017-12-27 978 views
0

重複條目,我們有兩個實體合併在manyto許多實體挑起jointable

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

日食鏈接嘗試複製在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 (?, ?)") 
..... 

任何提示的條目?該實體應該是正確的,我們合併所有者部分

回答

0

可能是您的List-s包含重複項。嘗試將List的集合類型更改爲Set。

+0

我不這麼認爲:要更新的數據在實體的適當字段中,並且與連接表無關。似乎它試圖再次保存已經持續檢查的列表 – user2946593

+0

,我們發現調用方法無法正常工作:重新填充列表之前調用的distributionlist.getContacts()。clear()沒有刪除任何內容。有必要分配一個新的變量get.Contacts()來讓clear()工作! – user2946593