mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
Do not use @View for Test
This commit is contained in:
@@ -4,8 +4,12 @@ import java.util.UUID;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.JoinColumn;
|
||||
import javax.persistence.ManyToOne;
|
||||
import javax.persistence.Table;
|
||||
|
||||
import io.ebean.annotation.DbForeignKey;
|
||||
|
||||
|
||||
@Entity
|
||||
@Table(name = "main_entity_relation")
|
||||
@@ -14,28 +18,34 @@ public class MainEntityRelation {
|
||||
@Id
|
||||
private UUID id;
|
||||
|
||||
private String id1;
|
||||
@ManyToOne
|
||||
@JoinColumn(name = "id1")
|
||||
@DbForeignKey(noConstraint = true)
|
||||
private MainEntity entity1;
|
||||
|
||||
private String id2;
|
||||
@ManyToOne
|
||||
@JoinColumn(name = "id2")
|
||||
@DbForeignKey(noConstraint = true)
|
||||
private MainEntity entity2;
|
||||
|
||||
private String attr1;
|
||||
|
||||
public String getId1() {
|
||||
return id1;
|
||||
}
|
||||
|
||||
public void setId1(String id1) {
|
||||
this.id1 = id1;
|
||||
}
|
||||
|
||||
public String getId2() {
|
||||
return id2;
|
||||
}
|
||||
|
||||
public void setId2(String id2) {
|
||||
this.id2 = id2;
|
||||
}
|
||||
|
||||
public MainEntity getEntity1() {
|
||||
return entity1;
|
||||
}
|
||||
|
||||
public void setEntity1(MainEntity entity1) {
|
||||
this.entity1 = entity1;
|
||||
}
|
||||
|
||||
public MainEntity getEntity2() {
|
||||
return entity2;
|
||||
}
|
||||
|
||||
public void setEntity2(MainEntity entity2) {
|
||||
this.entity2 = entity2;
|
||||
}
|
||||
|
||||
public String getAttr1() {
|
||||
return attr1;
|
||||
}
|
||||
|
||||
@@ -26,10 +26,16 @@ public class TestLazyForeignKeys extends BaseTestCase {
|
||||
DB.save(ent1);
|
||||
|
||||
MainEntityRelation rel1 = new MainEntityRelation();
|
||||
rel1.setId1("ent1");
|
||||
rel1.setId2("ent2");
|
||||
MainEntity e1 = new MainEntity();
|
||||
e1.setId("ent1");
|
||||
MainEntity e2 = new MainEntity();
|
||||
e2.setId("ent2");
|
||||
|
||||
rel1.setEntity1(e1);
|
||||
rel1.setEntity2(e2);
|
||||
DB.save(rel1);
|
||||
}
|
||||
|
||||
@After
|
||||
public void cleanup() {
|
||||
DB.find(MainEntity.class).delete();
|
||||
@@ -41,21 +47,22 @@ public class TestLazyForeignKeys extends BaseTestCase {
|
||||
// use findOne without select, so lazy loading will occur
|
||||
LoggedSqlCollector.start();
|
||||
|
||||
ViewMainEntityRelation vwRel1 = DB.find(ViewMainEntityRelation.class).findOne();
|
||||
MainEntityRelation rel1 = DB.find(MainEntityRelation.class).findOne();
|
||||
|
||||
assertEquals("ent1", vwRel1.getEntity1().getId());
|
||||
assertEquals("ent2", vwRel1.getEntity2().getId());
|
||||
assertEquals("ent1", rel1.getEntity1().getId());
|
||||
assertEquals("ent2", rel1.getEntity2().getId());
|
||||
|
||||
assertEquals("attr1", vwRel1.getEntity1().getAttr1());
|
||||
assertFalse(vwRel1.getEntity1().isDeleted());
|
||||
assertTrue(vwRel1.getEntity2().isDeleted());
|
||||
assertEquals("attr1", rel1.getEntity1().getAttr1());
|
||||
assertFalse(rel1.getEntity1().isDeleted());
|
||||
assertTrue(rel1.getEntity2().isDeleted());
|
||||
|
||||
List<String> loggedSql = LoggedSqlCollector.stop();
|
||||
assertThat(loggedSql).hasSize(3);
|
||||
assertThat(loggedSql.get(0)).contains("select t0.id, t0.attr1, t0.id1, t0.id2 from vw_main_entity_relation");
|
||||
assertThat(loggedSql.get(0)).contains("select t0.id, t0.attr1, t0.id1, t0.id2 from main_entity_relation");
|
||||
assertThat(loggedSql.get(1)).contains("select t0.id, t0.attr1, t0.attr2, t0.id is null from main_entity t0");
|
||||
assertThat(loggedSql.get(2)).contains("select t0.id, t0.attr1, t0.attr2, t0.id is null from main_entity t0");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFindListWithSelect() {
|
||||
PathProperties pathProp = new PathProperties();
|
||||
@@ -63,20 +70,19 @@ public class TestLazyForeignKeys extends BaseTestCase {
|
||||
pathProp.addToPath("entity1", "id");
|
||||
pathProp.addToPath("entity2", "id");
|
||||
|
||||
Query<ViewMainEntityRelation> query = Ebean.find(ViewMainEntityRelation.class).apply(pathProp);
|
||||
List<ViewMainEntityRelation> list = query.findList();
|
||||
Query<MainEntityRelation> query = Ebean.find(MainEntityRelation.class).apply(pathProp);
|
||||
List<MainEntityRelation> list = query.findList();
|
||||
assertEquals(1, list.size());
|
||||
|
||||
System.out.println(query.getGeneratedSql());
|
||||
assertThat(query.getGeneratedSql()).contains("t0.id, t0.attr1, t0.id1, t0.id2, t1.id, t2.id");
|
||||
|
||||
ViewMainEntityRelation vwRel1 = list.get(0);
|
||||
assertEquals("ent1", vwRel1.getEntity1().getId());
|
||||
assertEquals("ent2", vwRel1.getEntity2().getId());
|
||||
|
||||
assertEquals("attr1", vwRel1.getEntity1().getAttr1());
|
||||
assertFalse(vwRel1.getEntity1().isDeleted());
|
||||
assertTrue(vwRel1.getEntity2().isDeleted());
|
||||
MainEntityRelation rel1 = list.get(0);
|
||||
assertEquals("ent1", rel1.getEntity1().getId());
|
||||
assertEquals("ent2", rel1.getEntity2().getId());
|
||||
|
||||
assertEquals("attr1", rel1.getEntity1().getAttr1());
|
||||
assertFalse(rel1.getEntity1().isDeleted());
|
||||
assertTrue(rel1.getEntity2().isDeleted());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,42 +0,0 @@
|
||||
package org.tests.lazyforeignkeys;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
|
||||
import io.ebean.annotation.View;
|
||||
|
||||
@View(name = "vw_main_entity")
|
||||
@Entity
|
||||
public class ViewMainEntity {
|
||||
|
||||
@Id
|
||||
private String id;
|
||||
|
||||
private String attr1;
|
||||
|
||||
private String attr2;
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getAttr1() {
|
||||
return attr1;
|
||||
}
|
||||
|
||||
public void setAttr1(String attr1) {
|
||||
this.attr1 = attr1;
|
||||
}
|
||||
|
||||
public String getAttr2() {
|
||||
return attr2;
|
||||
}
|
||||
|
||||
public void setAttr2(String attr2) {
|
||||
this.attr2 = attr2;
|
||||
}
|
||||
}
|
||||
@@ -1,47 +0,0 @@
|
||||
package org.tests.lazyforeignkeys;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.JoinColumn;
|
||||
import javax.persistence.ManyToOne;
|
||||
|
||||
import io.ebean.annotation.DbForeignKey;
|
||||
import io.ebean.annotation.View;
|
||||
|
||||
@View(name = "vw_main_entity_relation")
|
||||
@Entity
|
||||
public class ViewMainEntityRelation {
|
||||
|
||||
@Id
|
||||
private UUID id;
|
||||
|
||||
@ManyToOne
|
||||
@JoinColumn(name = "id1")
|
||||
@DbForeignKey(noConstraint = true)
|
||||
private MainEntity entity1;
|
||||
|
||||
@ManyToOne
|
||||
@JoinColumn(name = "id2")
|
||||
@DbForeignKey(noConstraint = true)
|
||||
private MainEntity entity2;
|
||||
|
||||
private String attr1;
|
||||
|
||||
public String getAttr1() {
|
||||
return attr1;
|
||||
}
|
||||
|
||||
public void setAttr1(String attr1) {
|
||||
this.attr1 = attr1;
|
||||
}
|
||||
|
||||
public MainEntity getEntity1() {
|
||||
return entity1;
|
||||
}
|
||||
|
||||
public MainEntity getEntity2() {
|
||||
return entity2;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user