mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
#600 - Unique index or primary key violation - double insert attempted on ManyToMany intersection
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user