mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
Add more tests for cascade save on tree structure
This commit is contained in:
@@ -3,6 +3,7 @@ package org.tests.cascade;
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.Ebean;
|
||||
import io.ebean.Transaction;
|
||||
import org.ebeantest.LoggedSqlCollector;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
@@ -10,6 +11,11 @@ import org.tests.model.site.DataContainer;
|
||||
import org.tests.model.site.Site;
|
||||
import org.tests.model.site.SiteAddress;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class TestMultiCascadeBatch extends BaseTestCase {
|
||||
|
||||
private Transaction txn;
|
||||
@@ -26,6 +32,31 @@ public class TestMultiCascadeBatch extends BaseTestCase {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void save() {
|
||||
|
||||
Site grandparent = new Site();
|
||||
grandparent.setName("grandparent");
|
||||
Site parent = new Site();
|
||||
parent.setName("parent");
|
||||
Site child = new Site();
|
||||
child.setName("child");
|
||||
grandparent.setChildren(Collections.singletonList(parent));
|
||||
parent.setChildren(Collections.singletonList(child));
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
grandparent.save();
|
||||
|
||||
final List<String> sql = LoggedSqlCollector.stop();
|
||||
|
||||
assertThat(sql).hasSize(5);
|
||||
assertThat(sql.get(0)).contains("insert into site (id, name");
|
||||
assertThat(sql.get(1)).contains("insert into site (id, name");
|
||||
assertSqlBind(sql.get(2));
|
||||
assertThat(sql.get(3)).contains("insert into site (id, name");
|
||||
assertSqlBind(sql.get(4));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMultipleCascadeInsideTransaction() {
|
||||
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package org.tests.model.site;
|
||||
|
||||
import io.ebean.Model;
|
||||
|
||||
import javax.persistence.CascadeType;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
@@ -11,7 +13,7 @@ import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
@Entity
|
||||
public class Site {
|
||||
public class Site extends Model {
|
||||
|
||||
@Id
|
||||
private UUID id;
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
package org.tests.model.site;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import org.ebeantest.LoggedSqlCollector;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static java.util.Collections.singletonList;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class TestTreeModel extends BaseTestCase {
|
||||
|
||||
|
||||
@Test
|
||||
public void saveCascade() {
|
||||
|
||||
TreeEntity grandparent = new TreeEntity("grandparent");
|
||||
TreeEntity parent = new TreeEntity("parent");
|
||||
TreeEntity child = new TreeEntity("child");
|
||||
grandparent.setChildren(singletonList(parent));
|
||||
parent.setChildren(singletonList(child));
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
grandparent.save();
|
||||
|
||||
final List<String> sql = LoggedSqlCollector.stop();
|
||||
assertThat(sql).hasSize(5);
|
||||
assertThat(sql.get(0)).contains("insert into tree_entity");
|
||||
assertThat(sql.get(1)).contains("insert into tree_entity");
|
||||
assertThat(sql.get(3)).contains("insert into tree_entity");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
package org.tests.model.site;
|
||||
|
||||
import io.ebean.Model;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.ManyToOne;
|
||||
import javax.persistence.OneToMany;
|
||||
import java.util.List;
|
||||
|
||||
import static javax.persistence.CascadeType.ALL;
|
||||
|
||||
@Entity
|
||||
public class TreeEntity extends Model {
|
||||
|
||||
@Id
|
||||
private int id;
|
||||
|
||||
private String text;
|
||||
|
||||
@ManyToOne
|
||||
private TreeEntity parent;
|
||||
|
||||
@OneToMany(cascade = ALL)
|
||||
private List<TreeEntity> children;
|
||||
|
||||
public TreeEntity(String text) {
|
||||
this.text = text;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getText() {
|
||||
return text;
|
||||
}
|
||||
|
||||
public TreeEntity getParent() {
|
||||
return parent;
|
||||
}
|
||||
|
||||
public void setParent(TreeEntity parent) {
|
||||
this.parent = parent;
|
||||
}
|
||||
|
||||
public List<TreeEntity> getChildren() {
|
||||
return children;
|
||||
}
|
||||
|
||||
public void setChildren(List<TreeEntity> children) {
|
||||
this.children = children;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user