mirror of
https://github.com/ebean-orm/ebean.git
synced 2026-11-19 23:02:24 +00:00
Refactor move tests from ebean-core to ebean-test
This commit is contained in:
1 parent
0b00a14fd3
commit
eab56d323b
1650 files changed
+185
-147
No files matched your search
@@ -0,0 +1,38 @@
|
||||
package org.tests.o2m;
|
||||
|
||||
import javax.persistence.CascadeType;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.OneToMany;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
@Entity
|
||||
public class OMVertex {
|
||||
|
||||
@Id
|
||||
private UUID id;
|
||||
|
||||
@OneToMany(cascade = CascadeType.ALL)
|
||||
private List<OMVertexOther> related;
|
||||
|
||||
public OMVertex(UUID id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public UUID getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(UUID id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public List<OMVertexOther> getRelated() {
|
||||
return related;
|
||||
}
|
||||
|
||||
public void setRelated(List<OMVertexOther> related) {
|
||||
this.related = related;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package org.tests.o2m;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import java.util.UUID;
|
||||
|
||||
@Entity
|
||||
public class OMVertexOther {
|
||||
|
||||
@Id
|
||||
private UUID id;
|
||||
|
||||
private final String name;
|
||||
|
||||
public OMVertexOther(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package org.tests.o2m;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.ManyToOne;
|
||||
|
||||
@Entity
|
||||
public class OmAccountChildDBO {
|
||||
|
||||
@Id
|
||||
private long id;
|
||||
|
||||
private final String description;
|
||||
|
||||
@ManyToOne
|
||||
private OmAccountDBO bananaRama;
|
||||
|
||||
public OmAccountChildDBO(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public OmAccountDBO getBananaRama() {
|
||||
return bananaRama;
|
||||
}
|
||||
|
||||
public void setBananaRama(OmAccountDBO bananaRama) {
|
||||
this.bananaRama = bananaRama;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package org.tests.o2m;
|
||||
|
||||
import io.ebean.Model;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.OneToMany;
|
||||
import java.util.List;
|
||||
|
||||
import static javax.persistence.CascadeType.ALL;
|
||||
|
||||
@Entity
|
||||
public class OmAccountDBO extends Model {
|
||||
|
||||
@Id
|
||||
private long id;
|
||||
|
||||
private final String name;
|
||||
|
||||
@OneToMany(cascade = ALL, orphanRemoval = true)//, mappedBy = "bananaRama")
|
||||
private List<OmAccountChildDBO> child676;
|
||||
|
||||
public OmAccountDBO(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<OmAccountChildDBO> getChild676() {
|
||||
return child676;
|
||||
}
|
||||
|
||||
public void setChild676(List<OmAccountChildDBO> child676) {
|
||||
this.child676 = child676;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package org.tests.o2m;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.ManyToOne;
|
||||
import javax.persistence.Version;
|
||||
|
||||
@Entity
|
||||
public class OmBasicChild {
|
||||
|
||||
@Id
|
||||
private long id;
|
||||
|
||||
private final String name;
|
||||
|
||||
@ManyToOne
|
||||
private final OmBasicParent parent;
|
||||
|
||||
@Version
|
||||
private long version;
|
||||
|
||||
public OmBasicChild(String name, OmBasicParent parent) {
|
||||
this.name = name;
|
||||
this.parent = parent;
|
||||
}
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public OmBasicParent getParent() {
|
||||
return parent;
|
||||
}
|
||||
|
||||
public long getVersion() {
|
||||
return version;
|
||||
}
|
||||
|
||||
public void setVersion(long version) {
|
||||
this.version = version;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
package org.tests.o2m;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.OneToMany;
|
||||
import javax.persistence.Version;
|
||||
|
||||
import io.ebean.annotation.Where;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static javax.persistence.CascadeType.ALL;
|
||||
|
||||
@Entity
|
||||
public class OmBasicParent {
|
||||
|
||||
@Id
|
||||
private long id;
|
||||
|
||||
private final String name;
|
||||
|
||||
@Version
|
||||
private long version;
|
||||
|
||||
/**
|
||||
* Not really sensible Java code here but Kotlin can generate
|
||||
* a collection type that looks like this
|
||||
*/
|
||||
@OneToMany(cascade = ALL, mappedBy = "parent")
|
||||
private List<? extends OmBasicChild> children;
|
||||
|
||||
@OneToMany(cascade = ALL, mappedBy = "parent")
|
||||
@Where(clause = "'${dbTableName}' = ${ta}.name")
|
||||
private List<? extends OmBasicChild> childrenWithWhere;
|
||||
|
||||
public OmBasicParent(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public long getVersion() {
|
||||
return version;
|
||||
}
|
||||
|
||||
public void setVersion(long version) {
|
||||
this.version = version;
|
||||
}
|
||||
|
||||
public List<? extends OmBasicChild> getChildren() {
|
||||
return children;
|
||||
}
|
||||
|
||||
public void setChildren(List<? extends OmBasicChild> children) {
|
||||
this.children = children;
|
||||
}
|
||||
|
||||
public List<? extends OmBasicChild> getChildrenWithWhere() {
|
||||
return childrenWithWhere;
|
||||
}
|
||||
|
||||
public void setChildrenWithWhere(List<? extends OmBasicChild> childrenWithWhere) {
|
||||
this.childrenWithWhere = childrenWithWhere;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package org.tests.o2m;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import org.ebeantest.LoggedSqlCollector;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertSame;
|
||||
|
||||
public class TestOneToManyNoMappedBy extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
OmAccountDBO account = new OmAccountDBO("one");
|
||||
account.getChild676().add(new OmAccountChildDBO("child0"));
|
||||
account.getChild676().add(new OmAccountChildDBO("child1"));
|
||||
|
||||
account.save();
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
|
||||
final OmAccountDBO found = DB.find(OmAccountDBO.class)
|
||||
.where().eq("name", "one")
|
||||
.findOne();
|
||||
|
||||
final List<OmAccountChildDBO> children = found.getChild676();
|
||||
for (OmAccountChildDBO child : children) {
|
||||
final String description = child.getDescription();
|
||||
assertNotNull(description);
|
||||
assertSame(found, child.getBananaRama());
|
||||
}
|
||||
|
||||
final List<String> sql = LoggedSqlCollector.stop();
|
||||
assertThat(sql).hasSize(2);
|
||||
assertSql(sql.get(1)).contains("select t0.banana_rama_id, t0.id, t0.description, t0.banana_rama_id from om_account_child_dbo t0 where");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package org.tests.o2m;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.bean.EntityBean;
|
||||
import io.ebeaninternal.server.deploy.BeanDescriptor;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
public class TestOneToManyOnlyBean extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void insert() {
|
||||
|
||||
OMVertex myBean = new OMVertex(UUID.randomUUID());
|
||||
DB.save(myBean);
|
||||
|
||||
assertEquals(DB.find(OMVertex.class, myBean.getId()), myBean);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isReference_when_newWithId_expectFalse() {
|
||||
|
||||
OMVertex myBean = new OMVertex(UUID.randomUUID());
|
||||
assertFalse(isReference(myBean));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isReference_when_getReference_expectTrue() {
|
||||
|
||||
OMVertex myBean = DB.reference(OMVertex.class, UUID.randomUUID());
|
||||
assertTrue(isReference(myBean));
|
||||
}
|
||||
|
||||
private boolean isReference(Object myBean) {
|
||||
final BeanDescriptor<OMVertex> desc = getBeanDescriptor(OMVertex.class);
|
||||
return desc.isReference(((EntityBean) myBean)._ebean_getIntercept());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package org.tests.o2m;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebeantest.LoggedSql;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class TestOneToManyWhere extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void testWithDbTableName() {
|
||||
LoggedSql.start();
|
||||
DB.find(OmBasicParent.class).where().isNotNull("childrenWithWhere.name").findList();
|
||||
List<String> sql = LoggedSql.stop();
|
||||
assertThat(sql).hasSize(1);
|
||||
assertThat(sql.get(0)).contains("'om_basic_parent' = u1.name");
|
||||
|
||||
LoggedSql.start();
|
||||
DB.find(OmBasicParent.class).where().isNotEmpty("childrenWithWhere").findList();
|
||||
sql = LoggedSql.stop();
|
||||
assertThat(sql).hasSize(1);
|
||||
assertThat(sql.get(0)).contains("'om_basic_parent' = x.name");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLazyLoad() throws Exception {
|
||||
OmBasicParent el = new OmBasicParent("testLazyLoad");
|
||||
DB.save(el);
|
||||
LoggedSql.start();
|
||||
el = DB.find(OmBasicParent.class).select("name").where().eq("name", "testLazyLoad").findOne();
|
||||
el.getChildrenWithWhere().size(); // trigger Lazy load
|
||||
List<String> sql = LoggedSql.stop();
|
||||
assertThat(sql).hasSize(2);
|
||||
assertThat(sql.get(0)).contains("select t0.id, t0.name from om_basic_parent");
|
||||
assertThat(sql.get(1)).contains("where 'om_basic_parent' = t0.name");
|
||||
DB.delete(el);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package org.tests.o2m;
|
||||
|
||||
import io.ebean.DB;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class TestOneToManyWildcard {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
OmBasicParent parent = new OmBasicParent("p1");
|
||||
OmBasicChild c1 = new OmBasicChild("c1", parent);
|
||||
OmBasicChild c2 = new OmBasicChild("c2", parent);
|
||||
|
||||
DB.save(parent);
|
||||
DB.saveAll(Arrays.asList(c1, c2));
|
||||
|
||||
// we can't add to this in Java but Kotlin can
|
||||
//parent.getChildren().add(c1);
|
||||
|
||||
// exercise
|
||||
final OmBasicParent found = DB.find(OmBasicParent.class)
|
||||
.where().idEq(parent.getId())
|
||||
.findOne();
|
||||
|
||||
assertThat(found).isNotNull();
|
||||
assertThat(found.getChildren()).hasSize(2);
|
||||
|
||||
DB.delete(parent);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
package org.tests.o2m.jointable;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Table;
|
||||
import javax.persistence.Version;
|
||||
|
||||
@Entity
|
||||
@Table(name="monkey")
|
||||
public class JtMonkey {
|
||||
|
||||
@Id
|
||||
long mid;
|
||||
|
||||
String name;
|
||||
|
||||
String foodPreference;
|
||||
|
||||
@Version
|
||||
long version;
|
||||
|
||||
public JtMonkey(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public long getMid() {
|
||||
return mid;
|
||||
}
|
||||
|
||||
public void setMid(long mid) {
|
||||
this.mid = mid;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getFoodPreference() {
|
||||
return foodPreference;
|
||||
}
|
||||
|
||||
public void setFoodPreference(String foodPreference) {
|
||||
this.foodPreference = foodPreference;
|
||||
}
|
||||
|
||||
public long getVersion() {
|
||||
return version;
|
||||
}
|
||||
|
||||
public void setVersion(long version) {
|
||||
this.version = version;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
package org.tests.o2m.jointable;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.JoinTable;
|
||||
import javax.persistence.OneToMany;
|
||||
import javax.persistence.Table;
|
||||
import javax.persistence.Version;
|
||||
import java.util.List;
|
||||
|
||||
@Entity
|
||||
@Table(name="mkeygroup")
|
||||
public class JtMonkeyGroup {
|
||||
|
||||
@Id
|
||||
long pid;
|
||||
|
||||
String name;
|
||||
|
||||
/**
|
||||
* No cascading over to Monkey but we do maintain the join table regardless.
|
||||
*/
|
||||
@OneToMany
|
||||
@JoinTable
|
||||
List<JtMonkey> monkeys;
|
||||
|
||||
@Version
|
||||
long version;
|
||||
|
||||
public JtMonkeyGroup(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public long getPid() {
|
||||
return pid;
|
||||
}
|
||||
|
||||
public void setPid(long pid) {
|
||||
this.pid = pid;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public List<JtMonkey> getMonkeys() {
|
||||
return monkeys;
|
||||
}
|
||||
|
||||
public void setMonkeys(List<JtMonkey> monkeys) {
|
||||
this.monkeys = monkeys;
|
||||
}
|
||||
|
||||
public long getVersion() {
|
||||
return version;
|
||||
}
|
||||
|
||||
public void setVersion(long version) {
|
||||
this.version = version;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
package org.tests.o2m.jointable;
|
||||
|
||||
import javax.persistence.CascadeType;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.JoinTable;
|
||||
import javax.persistence.OneToMany;
|
||||
import javax.persistence.Table;
|
||||
import javax.persistence.Version;
|
||||
import java.util.List;
|
||||
|
||||
@Entity
|
||||
@Table(name="trainer")
|
||||
public class JtTrainer {
|
||||
|
||||
@Id
|
||||
long tid;
|
||||
|
||||
String name;
|
||||
|
||||
/**
|
||||
* Cascade so maintain join table and save any dirty Monkey beans.
|
||||
*/
|
||||
@OneToMany(cascade = CascadeType.PERSIST)
|
||||
@JoinTable(name = "trainer_monkey")
|
||||
List<JtMonkey> monkeys;
|
||||
|
||||
@Version
|
||||
long version;
|
||||
|
||||
public JtTrainer(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public long getTid() {
|
||||
return tid;
|
||||
}
|
||||
|
||||
public void setTid(long tid) {
|
||||
this.tid = tid;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public List<JtMonkey> getMonkeys() {
|
||||
return monkeys;
|
||||
}
|
||||
|
||||
public void setMonkeys(List<JtMonkey> monkeys) {
|
||||
this.monkeys = monkeys;
|
||||
}
|
||||
|
||||
public long getVersion() {
|
||||
return version;
|
||||
}
|
||||
|
||||
public void setVersion(long version) {
|
||||
this.version = version;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
package org.tests.o2m.jointable;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.JoinTable;
|
||||
import javax.persistence.OneToMany;
|
||||
import javax.persistence.Table;
|
||||
import javax.persistence.Version;
|
||||
import java.util.List;
|
||||
|
||||
@Entity
|
||||
@Table(name="troop")
|
||||
public class JtTroop {
|
||||
|
||||
@Id
|
||||
long pid;
|
||||
|
||||
String name;
|
||||
|
||||
/**
|
||||
* No cascading over to Monkey but we do maintain the join table regardless.
|
||||
*/
|
||||
@OneToMany
|
||||
@JoinTable(name = "troop_monkey")
|
||||
List<JtMonkey> monkeys;
|
||||
|
||||
@Version
|
||||
long version;
|
||||
|
||||
public JtTroop(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public long getPid() {
|
||||
return pid;
|
||||
}
|
||||
|
||||
public void setPid(long pid) {
|
||||
this.pid = pid;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public List<JtMonkey> getMonkeys() {
|
||||
return monkeys;
|
||||
}
|
||||
|
||||
public void setMonkeys(List<JtMonkey> monkeys) {
|
||||
this.monkeys = monkeys;
|
||||
}
|
||||
|
||||
public long getVersion() {
|
||||
return version;
|
||||
}
|
||||
|
||||
public void setVersion(long version) {
|
||||
this.version = version;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,135 @@
|
||||
package org.tests.o2m.jointable;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import org.ebeantest.LoggedSqlCollector;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class TestOneToManyJoinTable extends BaseTestCase {
|
||||
|
||||
private JtTroop troop = new JtTroop("Blue");
|
||||
|
||||
private JtMonkey m0 = new JtMonkey("Sim");
|
||||
private JtMonkey m1 = new JtMonkey("Tim");
|
||||
private JtMonkey m2 = new JtMonkey("Uim");
|
||||
|
||||
private void initialInsert() {
|
||||
DB.saveAll(Arrays.asList(troop, m0, m1, m2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void base() {
|
||||
|
||||
initialInsert();
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
|
||||
// make m0 dirty ... but no cascade saved?
|
||||
m0.setFoodPreference("banana");
|
||||
troop.getMonkeys().add(m0);
|
||||
troop.getMonkeys().add(m1);
|
||||
|
||||
DB.save(troop);
|
||||
|
||||
List<String> sql = LoggedSqlCollector.current();
|
||||
if (isPersistBatchOnCascade()) {
|
||||
assertThat(sql).hasSize(3);
|
||||
assertSql(sql.get(0)).contains("insert into troop_monkey (troop_pid, monkey_mid) values (?, ?)");
|
||||
assertSqlBind(sql, 1, 2);
|
||||
|
||||
} else {
|
||||
assertThat(sql).hasSize(2);
|
||||
assertSql(sql.get(0)).contains("insert into troop_monkey (troop_pid, monkey_mid) values (?, ?)");
|
||||
assertSql(sql.get(1)).contains("insert into troop_monkey (troop_pid, monkey_mid) values (?, ?)");
|
||||
}
|
||||
|
||||
long intersectionRows = DB.sqlQuery("select count(*) as total from troop_monkey where troop_pid = ?")
|
||||
.setParameter(1, troop.getPid())
|
||||
.mapToScalar(Long.class).findOne();
|
||||
|
||||
assertThat(intersectionRows).isEqualTo(2L);
|
||||
|
||||
LoggedSqlCollector.current();
|
||||
JtTroop fetchTroop = DB.find(JtTroop.class)
|
||||
.fetch("monkeys")
|
||||
.where().idEq(troop.getPid())
|
||||
.findOne();
|
||||
|
||||
assertThat(fetchTroop.getMonkeys()).hasSize(2);
|
||||
|
||||
sql = LoggedSqlCollector.current();
|
||||
assertThat(sql).hasSize(1);
|
||||
assertSql(sql.get(0)).contains("from troop t0 left join troop_monkey t1z_ on t1z_.troop_pid = t0.pid left join monkey t1 on t1.mid = t1z_.monkey_mid where t0.pid = ?");
|
||||
assertSql(sql.get(0)).contains("select t0.pid, t0.name, t0.version, t1.mid, t1.name, t1.food_preference, t1.version");
|
||||
|
||||
DB.delete(troop);
|
||||
|
||||
sql = LoggedSqlCollector.stop();
|
||||
assertThat(sql).hasSize(2);
|
||||
assertSql(sql.get(0)).contains("delete from troop_monkey where troop_pid = ?");
|
||||
assertSql(sql.get(1)).contains("delete from troop where pid=? and version=?");
|
||||
|
||||
|
||||
insertWithCascade();
|
||||
}
|
||||
|
||||
private void insertWithCascade() {
|
||||
|
||||
JtTrainer trainer = new JtTrainer("Frank");
|
||||
|
||||
// make m2 dirty ... cascades to an update on Uim
|
||||
m2.setFoodPreference("Apple");
|
||||
trainer.getMonkeys().add(m2);
|
||||
trainer.getMonkeys().add(DB.reference(JtMonkey.class, m1.getMid()));
|
||||
trainer.getMonkeys().add(new JtMonkey("FAlp"));
|
||||
trainer.getMonkeys().add(new JtMonkey("FBet"));
|
||||
trainer.getMonkeys().add(new JtMonkey("FThe"));
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
DB.save(trainer);
|
||||
|
||||
List<String> sql = LoggedSqlCollector.current();
|
||||
|
||||
if (isPersistBatchOnCascade()) {
|
||||
assertThat(sql).hasSize(13);
|
||||
assertSql(sql.get(0)).contains("insert into trainer ");
|
||||
assertSql(sql.get(1)).contains("insert into monkey ");
|
||||
assertSqlBind(sql, 2, 4);
|
||||
assertThat(sql.get(5)).contains("update monkey set food_preference=?, version=? where mid=? and version=?");
|
||||
assertThat(sql.get(6)).contains("-- bind(");
|
||||
assertThat(sql.get(7)).contains("insert into trainer_monkey ");
|
||||
assertSqlBind(sql, 8, 12);
|
||||
|
||||
} else {
|
||||
assertThat(sql).hasSize(10);
|
||||
assertSql(sql.get(0)).contains("insert into trainer ");
|
||||
assertSql(sql.get(1)).contains("update monkey set food_preference=?, version=? where mid=? and version=?");
|
||||
assertSql(sql.get(2)).contains("insert into monkey ");
|
||||
assertThat(sql.get(5)).contains("insert into trainer_monkey ");
|
||||
assertThat(sql.get(9)).contains("insert into trainer_monkey ");
|
||||
}
|
||||
|
||||
|
||||
int intersectionRows = DB.sqlQuery("select count(*) as total from trainer_monkey where trainer_tid = ?")
|
||||
.setParameter(1, trainer.getTid())
|
||||
.findOne()
|
||||
.getInteger("total");
|
||||
|
||||
assertThat(intersectionRows).isEqualTo(5);
|
||||
|
||||
|
||||
LoggedSqlCollector.current();
|
||||
DB.delete(trainer);
|
||||
|
||||
sql = LoggedSqlCollector.stop();
|
||||
assertThat(sql).hasSize(2);
|
||||
assertSql(sql.get(0)).contains("delete from trainer_monkey where trainer_tid = ?");
|
||||
assertSql(sql.get(1)).contains("delete from trainer where tid=?");
|
||||
|
||||
}
|
||||
}
|
||||
+63
@@ -0,0 +1,63 @@
|
||||
package org.tests.o2m.jointable;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.config.dbplatform.IdType;
|
||||
|
||||
import org.ebeantest.LoggedSqlCollector;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.o2m.jointable.inheritance.ClassA;
|
||||
import org.tests.o2m.jointable.inheritance.ClassB;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class TestOneToManyJoinTableInheritance extends BaseTestCase {
|
||||
|
||||
private JtMonkey m0 = new JtMonkey("Sim");
|
||||
private JtMonkey m1 = new JtMonkey("Tim");
|
||||
private JtMonkey m2 = new JtMonkey("Uim");
|
||||
|
||||
private ClassA classA = new ClassA();
|
||||
private ClassB classB = new ClassB();
|
||||
|
||||
@Test
|
||||
public void testSave() {
|
||||
|
||||
classA.getMonkeys().add(m0);
|
||||
classA.getMonkeys().add(m1);
|
||||
classB.getMonkeys().add(m2);
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
|
||||
DB.saveAll(Arrays.asList(classA, classB));
|
||||
|
||||
List<String> sql = LoggedSqlCollector.current();
|
||||
|
||||
assertThat(sql).hasSize(11);
|
||||
assertSql(sql.get(0)).contains("insert into class_super ");
|
||||
if (idType() == IdType.IDENTITY) {
|
||||
assertSql(sql.get(1)).contains("-- bind(ClassA)");
|
||||
assertSql(sql.get(2)).contains("-- bind(ClassB)");
|
||||
}
|
||||
assertThat(sql.get(3)).contains("insert into monkey ");
|
||||
if (idType() == IdType.IDENTITY) {
|
||||
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 (?, ?)");
|
||||
}
|
||||
assertSqlBind(sql, 8, 10);
|
||||
|
||||
ClassA dbA = DB.find(ClassA.class, 1);
|
||||
ClassB dbB = DB.find(ClassB.class, 2);
|
||||
|
||||
assertThat(dbA.getMonkeys()).hasSize(2);
|
||||
assertThat(dbB.getMonkeys()).hasSize(1);
|
||||
assertThat(dbA.getMonkeys().get(0).name).isEqualTo("Sim");
|
||||
assertThat(dbA.getMonkeys().get(1).name).isEqualTo("Tim");
|
||||
assertThat(dbB.getMonkeys().get(0).name).isEqualTo("Uim");
|
||||
}
|
||||
}
|
||||
+80
@@ -0,0 +1,80 @@
|
||||
package org.tests.o2m.jointable;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.DB;
|
||||
import org.ebeantest.LoggedSqlCollector;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class TestOneToManyJoinTableNoTableName extends BaseTestCase {
|
||||
|
||||
private JtMonkeyGroup troop = new JtMonkeyGroup("Pink");
|
||||
|
||||
private JtMonkey m0 = new JtMonkey("Sim3");
|
||||
private JtMonkey m1 = new JtMonkey("Tim3");
|
||||
private JtMonkey m2 = new JtMonkey("Uim3");
|
||||
|
||||
private void initialInsert() {
|
||||
DB.saveAll(Arrays.asList(troop, m0, m1, m2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void base() {
|
||||
|
||||
initialInsert();
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
|
||||
// make m0 dirty ... but no cascade saved?
|
||||
m0.setFoodPreference("camera");
|
||||
troop.getMonkeys().add(m0);
|
||||
troop.getMonkeys().add(m1);
|
||||
|
||||
DB.save(troop);
|
||||
|
||||
List<String> sql = LoggedSqlCollector.current();
|
||||
if (isPersistBatchOnCascade()) {
|
||||
assertThat(sql).hasSize(3);
|
||||
assertSql(sql.get(0)).contains("insert into mkeygroup_monkey (mkeygroup_pid, monkey_mid) values (?, ?)");
|
||||
assertSqlBind(sql, 1, 2);
|
||||
} else {
|
||||
assertThat(sql).hasSize(2);
|
||||
assertSql(sql.get(0)).contains("insert into mkeygroup_monkey (mkeygroup_pid, monkey_mid) values (?, ?)");
|
||||
assertSql(sql.get(1)).contains("insert into mkeygroup_monkey (mkeygroup_pid, monkey_mid) values (?, ?)");
|
||||
}
|
||||
|
||||
int intersectionRows = DB.sqlQuery("select count(*) as total from mkeygroup_monkey where mkeygroup_pid = ?")
|
||||
.setParameter(troop.getPid())
|
||||
.findOne()
|
||||
.getInteger("total");
|
||||
|
||||
assertThat(intersectionRows).isEqualTo(2);
|
||||
|
||||
LoggedSqlCollector.current();
|
||||
JtMonkeyGroup fetchTroop = DB.find(JtMonkeyGroup.class)
|
||||
.fetch("monkeys")
|
||||
.where().idEq(troop.getPid())
|
||||
.findOne();
|
||||
|
||||
assertThat(fetchTroop.getMonkeys()).hasSize(2);
|
||||
|
||||
sql = LoggedSqlCollector.current();
|
||||
assertThat(sql).hasSize(1);
|
||||
assertSql(sql.get(0)).contains("from mkeygroup t0 left join mkeygroup_monkey t1z_ on t1z_.mkeygroup_pid = t0.pid left join monkey t1 on t1.mid = t1z_.monkey_mid where t0.pid = ?");
|
||||
assertSql(sql.get(0)).contains("select t0.pid, t0.name, t0.version, t1.mid, t1.name, t1.food_preference, t1.version");
|
||||
|
||||
DB.delete(troop);
|
||||
|
||||
sql = LoggedSqlCollector.stop();
|
||||
assertThat(sql).hasSize(2);
|
||||
assertSql(sql.get(0)).contains("delete from mkeygroup_monkey where mkeygroup_pid = ?");
|
||||
assertSql(sql.get(1)).contains("delete from mkeygroup where pid=? and version=?");
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package org.tests.o2m.jointable.inheritance;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
|
||||
@Entity
|
||||
public class ClassA extends ClassSuper {
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package org.tests.o2m.jointable.inheritance;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
|
||||
@Entity
|
||||
public class ClassB extends ClassSuper {
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package org.tests.o2m.jointable.inheritance;
|
||||
|
||||
import org.tests.o2m.jointable.JtMonkey;
|
||||
|
||||
import javax.persistence.CascadeType;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Inheritance;
|
||||
import javax.persistence.JoinTable;
|
||||
import javax.persistence.OneToMany;
|
||||
import java.util.List;
|
||||
|
||||
@Inheritance
|
||||
@Entity
|
||||
public abstract class ClassSuper {
|
||||
@Id
|
||||
long sid;
|
||||
|
||||
@JoinTable
|
||||
@OneToMany(cascade = CascadeType.ALL)
|
||||
private List<JtMonkey> monkeys;
|
||||
|
||||
public List<JtMonkey> getMonkeys() {
|
||||
return monkeys;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package org.tests.o2m.lazy;
|
||||
|
||||
import javax.persistence.CascadeType;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.OneToMany;
|
||||
import javax.persistence.Table;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Entity
|
||||
@Table(name = "oml_bar")
|
||||
public class OmlBar {
|
||||
|
||||
@Id
|
||||
private Long id;
|
||||
|
||||
@OneToMany(mappedBy = "bar", cascade = CascadeType.ALL)
|
||||
private List<OmlFoo> fooList = new ArrayList<OmlFoo>();
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public List<OmlFoo> getFooList() {
|
||||
return fooList;
|
||||
}
|
||||
|
||||
public void setFooList(List<OmlFoo> fooList) {
|
||||
this.fooList = fooList;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package org.tests.o2m.lazy;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.ManyToOne;
|
||||
import javax.persistence.Table;
|
||||
|
||||
@Entity
|
||||
@Table(name = "oml_baz")
|
||||
public class OmlBaz {
|
||||
@Id
|
||||
private Long id;
|
||||
|
||||
@ManyToOne(optional = false)
|
||||
private OmlFoo foo;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public OmlFoo getFoo() {
|
||||
return foo;
|
||||
}
|
||||
|
||||
public void setFoo(OmlFoo foo) {
|
||||
this.foo = foo;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package org.tests.o2m.lazy;
|
||||
|
||||
import javax.persistence.CascadeType;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.ManyToOne;
|
||||
import javax.persistence.OneToMany;
|
||||
import javax.persistence.Table;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Entity
|
||||
@Table(name = "oml_foo")
|
||||
public class OmlFoo {
|
||||
|
||||
@Id
|
||||
private Long id;
|
||||
|
||||
@ManyToOne(optional = false)
|
||||
private OmlBar bar;
|
||||
|
||||
@OneToMany(mappedBy = "foo", cascade = CascadeType.ALL)
|
||||
private List<OmlBaz> bazList = new ArrayList<OmlBaz>();
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public OmlBar getBar() {
|
||||
return bar;
|
||||
}
|
||||
|
||||
public void setBar(OmlBar bar) {
|
||||
this.bar = bar;
|
||||
}
|
||||
|
||||
public List<OmlBaz> getBazList() {
|
||||
return bazList;
|
||||
}
|
||||
|
||||
public void setBazList(List<OmlBaz> bazList) {
|
||||
this.bazList = bazList;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
package org.tests.o2m.lazy;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class TestLazyManyViaPath extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
OmlBar bar = new OmlBar();
|
||||
List<OmlFoo> fooList = new ArrayList<>();
|
||||
fooList.add(createNewFooWithBar(bar));
|
||||
|
||||
bar.setFooList(fooList);
|
||||
|
||||
DB.save(bar);
|
||||
|
||||
OmlFoo fooFromDb = DB
|
||||
.find(OmlFoo.class)
|
||||
.where()
|
||||
.eq("id", bar.getFooList().get(0).getId())
|
||||
.findOne();
|
||||
|
||||
// This works
|
||||
List<OmlFoo> foosList = fooFromDb.getBar().getFooList();
|
||||
assertThat(fooList.size()).isEqualTo(1);
|
||||
|
||||
OmlBaz bazFromDb = DB
|
||||
.find(OmlBaz.class)
|
||||
.where()
|
||||
.eq("id", bar.getFooList().get(0).getBazList().get(0).getId())
|
||||
.findOne();
|
||||
|
||||
// This does not work and gives the exception
|
||||
List<OmlFoo> foosList1 = bazFromDb.getFoo().getBar().getFooList();
|
||||
assertThat(foosList1.size()).isEqualTo(1);
|
||||
|
||||
}
|
||||
|
||||
private static OmlFoo createNewFooWithBar(OmlBar bar) {
|
||||
OmlFoo foo = new OmlFoo();
|
||||
foo.setBar(bar);
|
||||
OmlBaz baz = new OmlBaz();
|
||||
baz.setFoo(foo);
|
||||
foo.getBazList().add(baz);
|
||||
return foo;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user