mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
* provide test case, that shows problem on multiple-m2m * FIX: When saving multiple m2m beans, only last m2m wins (Regression due #1630)
This commit is contained in:
committed by
Rob Bygrave
parent
7929d8c0b8
commit
fc7fae35c9
@@ -6,11 +6,13 @@ import io.ebean.Ebean;
|
||||
import io.ebean.Transaction;
|
||||
import org.junit.Test;
|
||||
import org.tests.sp.model.car.Car;
|
||||
import org.tests.sp.model.car.Door;
|
||||
import org.tests.sp.model.car.Wheel;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
@@ -24,15 +26,24 @@ public class TestManyToManySaveTwice extends BaseTestCase {
|
||||
Wheel w0 = new Wheel("wx0");
|
||||
Wheel w1 = new Wheel("wx1");
|
||||
|
||||
Door d0 = new Door("dx0");
|
||||
Door d1 = new Door("dx1");
|
||||
|
||||
List<Wheel> wheels = new LinkedList<>();
|
||||
wheels.add(w0);
|
||||
wheels.add(w1);
|
||||
|
||||
List<Door> doors = new LinkedList<>();
|
||||
doors.add(d0);
|
||||
doors.add(d1);
|
||||
|
||||
DB.saveAll(wheels);
|
||||
DB.saveAll(doors);
|
||||
|
||||
Car c0 = new Car("cx0");
|
||||
c0.getWheels().add(w0);
|
||||
c0.getWheels().add(w1);
|
||||
c0.getDoors().add(d0);
|
||||
|
||||
Car c1 = new Car("cx1");
|
||||
c1.getWheels().add(w1);
|
||||
@@ -45,6 +56,16 @@ public class TestManyToManySaveTwice extends BaseTestCase {
|
||||
|
||||
transaction.commit();
|
||||
}
|
||||
|
||||
c0 = Ebean.find(Car.class, c0.getId());
|
||||
c1 = Ebean.find(Car.class, c1.getId());
|
||||
|
||||
assertThat(c0.getWheels()).hasSize(2);
|
||||
assertThat(c0.getDoors()).hasSize(1);
|
||||
|
||||
assertThat(c1.getWheels()).hasSize(1);
|
||||
assertThat(c1.getDoors()).hasSize(0);
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -101,7 +122,9 @@ public class TestManyToManySaveTwice extends BaseTestCase {
|
||||
|
||||
private void delete() {
|
||||
DB.sqlUpdate("delete from sp_car_car_wheels").execute();
|
||||
DB.sqlUpdate("delete from sp_car_car_doors").execute();
|
||||
DB.sqlUpdate("delete from sp_car_wheel").execute();
|
||||
DB.sqlUpdate("delete from sp_car_door").execute();
|
||||
DB.sqlUpdate("delete from sp_car_car").execute();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,6 +22,10 @@ public class Car extends IdEntity {
|
||||
@JoinTable(name = "sp_car_car_wheels", joinColumns = {@JoinColumn(name = "car")}, inverseJoinColumns = {@JoinColumn(name = "wheel")})
|
||||
private List<Wheel> wheels;
|
||||
|
||||
@ManyToMany(cascade = CascadeType.ALL)
|
||||
@JoinTable(name = "sp_car_car_doors", joinColumns = {@JoinColumn(name = "car")}, inverseJoinColumns = {@JoinColumn(name = "door")})
|
||||
private List<Door> doors;
|
||||
|
||||
public Car(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
@@ -37,4 +41,12 @@ public class Car extends IdEntity {
|
||||
public void setWheels(List<Wheel> wheels) {
|
||||
this.wheels = wheels;
|
||||
}
|
||||
|
||||
public List<Door> getDoors() {
|
||||
return doors;
|
||||
}
|
||||
|
||||
public void setDoors(List<Door> doors) {
|
||||
this.doors = doors;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
package org.tests.sp.model.car;
|
||||
|
||||
import org.tests.sp.model.IdEntity;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Table;
|
||||
|
||||
@Entity
|
||||
@Table(name = "sp_car_door")
|
||||
public class Door extends IdEntity {
|
||||
|
||||
private static final long serialVersionUID = 2399600193947163469L;
|
||||
|
||||
private String name;
|
||||
|
||||
public Door(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user