mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
no ebean code change: extended test case to use a bean in @Formula and return ID
This commit is contained in:
@@ -8,6 +8,7 @@ import java.util.List;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.avaje.tests.lib.EbeanTestCase;
|
||||
import com.avaje.tests.model.basic.EBasic;
|
||||
import com.avaje.tests.model.family.ChildPerson;
|
||||
import com.avaje.tests.model.family.GrandParentPerson;
|
||||
import com.avaje.tests.model.family.ParentPerson;
|
||||
@@ -19,7 +20,15 @@ public class TestInheritance extends EbeanTestCase {
|
||||
|
||||
@Test
|
||||
public void testInheritance() {
|
||||
|
||||
// create some other beans
|
||||
EBasic someBean1 = new EBasic();
|
||||
someBean1.setName("A Bean");
|
||||
getServer().save(someBean1);
|
||||
|
||||
EBasic someBean2 = new EBasic();
|
||||
someBean2.setName("An other Bean");
|
||||
getServer().save(someBean2);
|
||||
|
||||
ChildPerson child1 = new ChildPerson();
|
||||
child1.setAge(13);
|
||||
child1.setName("Fred");
|
||||
@@ -37,6 +46,8 @@ public class TestInheritance extends EbeanTestCase {
|
||||
parent1.setAge(40);
|
||||
parent1.setName("Maria");
|
||||
parent1.setFamilyName("Bar");
|
||||
parent1.setSomeBean(someBean1);
|
||||
child1.setSomeBean(someBean2);
|
||||
parent1.getChildren().add(child1);
|
||||
parent1.getChildren().add(child2);
|
||||
|
||||
@@ -74,6 +85,7 @@ public class TestInheritance extends EbeanTestCase {
|
||||
assertEquals("Josef", grandparent1.getName());
|
||||
assertEquals("Foo", grandparent1.getFamilyName());
|
||||
assertEquals("Munich", grandparent1.getAddress());
|
||||
assertNull(grandparent1.getEffectiveBeanId());
|
||||
|
||||
// now check children of grandparent
|
||||
parent1 = grandparent1.getChildren().get(0);
|
||||
@@ -91,6 +103,7 @@ public class TestInheritance extends EbeanTestCase {
|
||||
assertEquals("Munich", parent1.getEffectiveAddress()); // -> inherit munich
|
||||
assertEquals(2, parent1.getChildCount().intValue());
|
||||
assertEquals(21, parent1.getTotalAge().intValue());
|
||||
assertEquals(1, parent1.getEffectiveBeanId().intValue());
|
||||
|
||||
// parent2
|
||||
assertEquals("Sandra", parent2.getName());
|
||||
@@ -100,6 +113,7 @@ public class TestInheritance extends EbeanTestCase {
|
||||
assertEquals("Berlin", parent2.getEffectiveAddress());
|
||||
assertEquals(1, parent2.getChildCount().intValue());
|
||||
assertEquals(36, parent2.getTotalAge().intValue());
|
||||
assertNull(parent2.getEffectiveBeanId());
|
||||
|
||||
// parent3
|
||||
assertEquals("Michael", parent3.getName());
|
||||
@@ -109,7 +123,7 @@ public class TestInheritance extends EbeanTestCase {
|
||||
assertEquals("Munich", parent3.getEffectiveAddress());
|
||||
assertEquals(0, parent3.getChildCount().intValue());
|
||||
assertEquals(0, parent3.getTotalAge().intValue());
|
||||
|
||||
assertNull(parent3.getEffectiveBeanId());
|
||||
|
||||
child1 = parent1.getChildren().get(0);
|
||||
child2 = parent1.getChildren().get(1);
|
||||
@@ -122,12 +136,17 @@ public class TestInheritance extends EbeanTestCase {
|
||||
|
||||
assertEquals("Bar", child1.getEffectiveFamilyName());
|
||||
assertEquals("Munich", child1.getEffectiveAddress());
|
||||
assertEquals(2, child1.getSomeBean().getId().intValue());
|
||||
assertEquals(2, child1.getEffectiveBeanId().intValue());
|
||||
|
||||
assertEquals("Baz", child2.getEffectiveFamilyName());
|
||||
assertEquals("Munich", child2.getEffectiveAddress());
|
||||
assertNull(child2.getSomeBean());
|
||||
assertEquals(1, child2.getEffectiveBeanId().intValue());
|
||||
|
||||
assertEquals("Foo", child3.getEffectiveFamilyName());
|
||||
assertEquals("Berlin", child3.getEffectiveAddress());
|
||||
assertNull(child3.getEffectiveBeanId());
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,6 +28,8 @@ public class ChildPerson extends InheritablePerson {
|
||||
@Formula(select = "coalesce(${ta}.address, j1.address, j2.address)", join = PARENTS_JOIN )
|
||||
private String effectiveAddress;
|
||||
|
||||
@Formula(select = "coalesce(${ta}.some_bean_id, j1.some_bean_id, j2.some_bean_id)")
|
||||
private Integer effectiveBeanId;
|
||||
|
||||
public ParentPerson getParent() {
|
||||
return parent;
|
||||
@@ -61,6 +63,9 @@ public class ChildPerson extends InheritablePerson {
|
||||
return effectiveAddress;
|
||||
}
|
||||
|
||||
public Integer getEffectiveBeanId() {
|
||||
return effectiveBeanId;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -33,6 +33,10 @@ public class GrandParentPerson extends InheritablePerson {
|
||||
|
||||
private String address;
|
||||
|
||||
|
||||
@Formula(select = "${ta}.some_bean_id")
|
||||
private Integer effectiveBeanId;
|
||||
|
||||
public List<ParentPerson> getChildren() {
|
||||
return children;
|
||||
}
|
||||
@@ -66,5 +70,7 @@ public class GrandParentPerson extends InheritablePerson {
|
||||
return childCount;
|
||||
}
|
||||
|
||||
|
||||
public Integer getEffectiveBeanId() {
|
||||
return effectiveBeanId;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,8 +2,11 @@
|
||||
package com.avaje.tests.model.family;
|
||||
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.ManyToOne;
|
||||
import javax.persistence.MappedSuperclass;
|
||||
|
||||
import com.avaje.tests.model.basic.EBasic;
|
||||
|
||||
@MappedSuperclass
|
||||
public abstract class InheritablePerson {
|
||||
|
||||
@@ -14,6 +17,9 @@ public abstract class InheritablePerson {
|
||||
|
||||
private Integer age;
|
||||
|
||||
@ManyToOne
|
||||
private EBasic someBean;
|
||||
|
||||
public Integer getIdentifier() {
|
||||
return identifier;
|
||||
}
|
||||
@@ -37,7 +43,13 @@ public abstract class InheritablePerson {
|
||||
public void setAge(Integer age) {
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public void setSomeBean(EBasic someBean) {
|
||||
this.someBean = someBean;
|
||||
}
|
||||
|
||||
public EBasic getSomeBean() {
|
||||
return someBean;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -48,6 +48,9 @@ public class ParentPerson extends InheritablePerson {
|
||||
@Formula(select = "coalesce(${ta}.address, j1.address)", join = GRAND_PARENT_PERSON_JOIN )
|
||||
private String effectiveAddress;
|
||||
|
||||
@Formula(select = "coalesce(${ta}.some_bean_id, j1.some_bean_id)")
|
||||
private Integer effectiveBeanId;
|
||||
|
||||
public GrandParentPerson getParent() {
|
||||
return parent;
|
||||
}
|
||||
@@ -92,5 +95,8 @@ public class ParentPerson extends InheritablePerson {
|
||||
return effectiveAddress;
|
||||
}
|
||||
|
||||
public Integer getEffectiveBeanId() {
|
||||
return effectiveBeanId;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user