mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
#1833 - When cascade persisting twice to ElementCollection ... we get do extra delete and inserts
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
package org.tests.model.elementcollection;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.ManyToMany;
|
||||
import javax.persistence.ManyToOne;
|
||||
import javax.persistence.Version;
|
||||
import java.util.List;
|
||||
|
||||
import static javax.persistence.CascadeType.ALL;
|
||||
|
||||
@Entity
|
||||
public class EcTop {
|
||||
|
||||
@Id
|
||||
private long id;
|
||||
|
||||
@Version
|
||||
private long version;
|
||||
|
||||
private final String name;
|
||||
|
||||
@ManyToOne(cascade = ALL)
|
||||
private EcsPerson person;
|
||||
|
||||
/**
|
||||
* Not normally expect cascade on ManyToMany but here for this test.
|
||||
*/
|
||||
@ManyToMany(cascade = ALL)
|
||||
private List<EcsPerson> people;
|
||||
|
||||
public EcTop(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public long getVersion() {
|
||||
return version;
|
||||
}
|
||||
|
||||
public void setVersion(long version) {
|
||||
this.version = version;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public EcsPerson getPerson() {
|
||||
return person;
|
||||
}
|
||||
|
||||
public void setPerson(EcsPerson person) {
|
||||
this.person = person;
|
||||
}
|
||||
|
||||
public List<EcsPerson> getPeople() {
|
||||
return people;
|
||||
}
|
||||
|
||||
public void setPeople(List<EcsPerson> people) {
|
||||
this.people = people;
|
||||
}
|
||||
}
|
||||
+51
@@ -0,0 +1,51 @@
|
||||
package org.tests.model.elementcollection;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.annotation.Transactional;
|
||||
import org.ebeantest.LoggedSqlCollector;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class TestElementCollectionCascadeMultiple extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
EcTop top = new EcTop("top0");
|
||||
|
||||
EcsPerson person = new EcsPerson("Ethan027");
|
||||
person.getPhoneNumbers().add("027 1234");
|
||||
person.getPhoneNumbers().add("027 4321");
|
||||
|
||||
top.setPerson(person);
|
||||
top.getPeople().add(person);
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
|
||||
save(top);
|
||||
|
||||
final List<String> sql = LoggedSqlCollector.stop();
|
||||
|
||||
assertThat(sql.get(0)).contains("insert into ecs_person");
|
||||
assertThat(sql.get(1)).contains("-- bind");
|
||||
assertThat(sql.get(2)).contains("insert into ecs_person_phone");
|
||||
assertThat(sql.get(3)).contains("-- bind");
|
||||
assertThat(sql.get(4)).contains("-- bind");
|
||||
assertThat(sql.get(5)).contains("insert into ec_top");
|
||||
assertThat(sql.get(6)).contains("-- bind");
|
||||
assertThat(sql.get(7)).contains("insert into ec_top_ecs_person");
|
||||
assertThat(sql.get(8)).contains("-- bind");
|
||||
|
||||
assertThat(sql).hasSize(9);
|
||||
}
|
||||
|
||||
@Transactional(batchSize = 20)
|
||||
public void save(EcTop top) {
|
||||
DB.save(top);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user