mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
#1630 - ENH: Improve jdbc batch use when persisting ManyToMany intersection changes
This commit is contained in:
@@ -47,12 +47,11 @@ public class TestOneToManyJoinTable extends BaseTestCase {
|
||||
assertThat(sql.get(1)).contains("insert into troop_monkey (troop_pid, monkey_mid) values (?, ?)");
|
||||
}
|
||||
|
||||
int intersectionRows = Ebean.createSqlQuery("select count(*) as total from troop_monkey where troop_pid = ?")
|
||||
long intersectionRows = Ebean.createSqlQuery("select count(*) as total from troop_monkey where troop_pid = ?")
|
||||
.setParameter(1, troop.getPid())
|
||||
.findOne()
|
||||
.getInteger("total");
|
||||
.findSingleLong();
|
||||
|
||||
assertThat(intersectionRows).isEqualTo(2);
|
||||
assertThat(intersectionRows).isEqualTo(2L);
|
||||
|
||||
LoggedSqlCollector.current();
|
||||
JtTroop fetchTroop = Ebean.find(JtTroop.class)
|
||||
|
||||
@@ -34,14 +34,15 @@ public class TestOneToManyJoinTableInheritance extends BaseTestCase {
|
||||
|
||||
List<String> sql = LoggedSqlCollector.current();
|
||||
|
||||
assertThat(sql).hasSize(11);
|
||||
assertThat(sql).hasSize(8);
|
||||
assertThat(sql.get(0)).contains("insert into class_super ");
|
||||
assertThat(sql.get(2)).contains("insert into monkey ");
|
||||
assertThat(sql.get(2)).contains("insert into monkey ");
|
||||
assertThat(sql.get(5)).contains("insert into class_super_monkey ");
|
||||
assertThat(sql.get(6)).contains("insert into class_super ");
|
||||
assertThat(sql.get(8)).contains("insert into monkey ");
|
||||
assertThat(sql.get(10)).contains("insert into class_super_monkey ");
|
||||
assertThat(sql.get(1)).contains("-- bind(ClassA)");
|
||||
assertThat(sql.get(2)).contains("-- bind(ClassB)");
|
||||
assertThat(sql.get(3)).contains("insert into monkey ");
|
||||
assertThat(sql.get(4)).contains("-- bind(Sim");
|
||||
assertThat(sql.get(5)).contains("-- bind(Tim");
|
||||
assertThat(sql.get(6)).contains("-- bind(Uim");
|
||||
assertThat(sql.get(7)).contains("insert into class_super_monkey (class_super_sid, monkey_mid) values (?, ?)");
|
||||
|
||||
ClassA dbA = Ebean.find(ClassA.class, 1);
|
||||
ClassB dbB = Ebean.find(ClassB.class, 2);
|
||||
|
||||
@@ -1,43 +1,107 @@
|
||||
package org.tests.sp;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
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.Wheel;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
public class TestManyToManySaveTwice extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void insertBatch() {
|
||||
|
||||
delete();
|
||||
|
||||
Wheel w0 = new Wheel("wx0");
|
||||
Wheel w1 = new Wheel("wx1");
|
||||
|
||||
List<Wheel> wheels = new LinkedList<>();
|
||||
wheels.add(w0);
|
||||
wheels.add(w1);
|
||||
|
||||
DB.saveAll(wheels);
|
||||
|
||||
Car c0 = new Car("cx0");
|
||||
c0.getWheels().add(w0);
|
||||
c0.getWheels().add(w1);
|
||||
|
||||
Car c1 = new Car("cx1");
|
||||
c1.getWheels().add(w1);
|
||||
|
||||
try (Transaction transaction = DB.beginTransaction()) {
|
||||
transaction.setBatchMode(true);
|
||||
|
||||
DB.save(c0);
|
||||
DB.save(c1);
|
||||
|
||||
transaction.commit();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void insertBasic() {
|
||||
|
||||
delete();
|
||||
|
||||
Wheel w0 = new Wheel("wx0");
|
||||
Wheel w1 = new Wheel("wx1");
|
||||
|
||||
List<Wheel> wheels = new LinkedList<>();
|
||||
wheels.add(w0);
|
||||
wheels.add(w1);
|
||||
|
||||
DB.saveAll(wheels);
|
||||
|
||||
Car c0 = new Car("cx0");
|
||||
c0.getWheels().add(w0);
|
||||
c0.getWheels().add(w1);
|
||||
|
||||
Car c1 = new Car("cx1");
|
||||
c1.getWheels().add(w1);
|
||||
|
||||
DB.save(c0);
|
||||
DB.save(c1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInsertCarTwice() {
|
||||
|
||||
Ebean.createSqlUpdate("delete from sp_car_car_wheels").execute();
|
||||
Ebean.createSqlUpdate("delete from sp_car_wheel").execute();
|
||||
Ebean.createSqlUpdate("delete from sp_car_car").execute();
|
||||
delete();
|
||||
|
||||
List<Wheel> wheels = new LinkedList<>();
|
||||
wheels.add(new Wheel());
|
||||
wheels.add(new Wheel());
|
||||
wheels.add(new Wheel());
|
||||
wheels.add(new Wheel());
|
||||
wheels.add(new Wheel("w0"));
|
||||
wheels.add(new Wheel("w1"));
|
||||
wheels.add(new Wheel("w2"));
|
||||
wheels.add(new Wheel("w3"));
|
||||
|
||||
Car c = new Car();
|
||||
Car c = new Car("c1");
|
||||
c.setWheels(wheels);
|
||||
|
||||
Ebean.save(c); // NOTE 1ST SAVE
|
||||
DB.save(c); // NOTE 1ST SAVE
|
||||
assertNotNull(c.getId());
|
||||
|
||||
Assert.assertFalse("No ID assigned!", c.getId() == null);
|
||||
|
||||
Ebean.save(c); // NOTE 2ND SAVE
|
||||
DB.save(c); // NOTE 2ND SAVE
|
||||
|
||||
List<Car> allCars = Ebean.find(Car.class).findList();
|
||||
Assert.assertEquals("Inserted 1 car, received more/less!", 1, allCars.size());
|
||||
assertEquals(1, allCars.size());
|
||||
|
||||
List<Wheel> allWheels = Ebean.find(Wheel.class).findList();
|
||||
Assert.assertEquals("Inserted 4 wheels, received more/less!", 4, allWheels.size());
|
||||
assertEquals(4, allWheels.size());
|
||||
}
|
||||
|
||||
private void delete() {
|
||||
DB.sqlUpdate("delete from sp_car_car_wheels").execute();
|
||||
DB.sqlUpdate("delete from sp_car_wheel").execute();
|
||||
DB.sqlUpdate("delete from sp_car_car").execute();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,10 +16,20 @@ public class Car extends IdEntity {
|
||||
|
||||
private static final long serialVersionUID = 2579148859565507940L;
|
||||
|
||||
private final String name;
|
||||
|
||||
@ManyToMany(cascade = CascadeType.ALL)
|
||||
@JoinTable(name = "sp_car_car_wheels", joinColumns = {@JoinColumn(name = "car")}, inverseJoinColumns = {@JoinColumn(name = "wheel")})
|
||||
private List<Wheel> wheels;
|
||||
|
||||
public Car(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public List<Wheel> getWheels() {
|
||||
return wheels;
|
||||
}
|
||||
|
||||
@@ -11,4 +11,13 @@ public class Wheel extends IdEntity {
|
||||
|
||||
private static final long serialVersionUID = 2399600193947163469L;
|
||||
|
||||
private String name;
|
||||
|
||||
public Wheel(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user