#600 - Unique index or primary key violation - double insert attempted on ManyToMany intersection

This commit is contained in:
Robin Bygrave
2016-03-14 22:32:03 +13:00
parent 9bcc8a77fe
commit c40715733e
5 changed files with 184 additions and 1 deletions
@@ -0,0 +1,49 @@
package com.avaje.tests.model.m2m;
import com.avaje.ebean.Model;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import java.util.List;
@Entity
public class DCredit extends Model {
@Id
Long id;
String credit;
@ManyToMany(cascade = CascadeType.PERSIST)
List<DRol> droles;
public DCredit(String credit) {
this.credit = credit;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getCredit() {
return credit;
}
public void setCredit(String credit) {
this.credit = credit;
}
public List<DRol> getDroles() {
return droles;
}
public void setDroles(List<DRol> droles) {
this.droles = droles;
}
}
@@ -0,0 +1,56 @@
package com.avaje.tests.model.m2m;
import com.avaje.ebean.Model;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import java.util.List;
@Entity
public class DRol extends Model {
@Id
Long id;
final String name;
@ManyToMany(mappedBy = "droles", cascade = CascadeType.PERSIST)
private List<DCredit> credits;
@ManyToMany(mappedBy = "croles", cascade = CascadeType.PERSIST)
private List<DRot> rots;
public DRol(String name) {
this.name = name;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public List<DCredit> getCredits() {
return credits;
}
public void setCredits(List<DCredit> credits) {
this.credits = credits;
}
public List<DRot> getRots() {
return rots;
}
public void setRots(List<DRot> rots) {
this.rots = rots;
}
}
@@ -0,0 +1,46 @@
package com.avaje.tests.model.m2m;
import com.avaje.ebean.Model;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import java.util.List;
@Entity
public class DRot extends Model {
@Id
Long id;
final String name;
@ManyToMany(cascade = CascadeType.ALL)
List<DRol> croles;
public DRot(String name) {
this.name = name;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public List<DRol> getCroles() {
return croles;
}
public void setCroles(List<DRol> croles) {
this.croles = croles;
}
}
@@ -0,0 +1,23 @@
package com.avaje.tests.model.m2m;
import com.avaje.ebean.Ebean;
import org.junit.Test;
public class TestM2MDoubleInsert {
@Test
public void doubleInsert() {
DRol role = new DRol("rol");
role.save();
DCredit credit = new DCredit("x1");
credit.getDroles().add(role);
role.getCredits().add(credit);
credit.save();
DRot rot = new DRot("rot");
rot.getCroles().add(role);
Ebean.save(rot);
}
}