FIX: do not hit database if inherited entity has no children (#974)

* FIX: do not hit database if inherited entity has no children

* ADD: Testcase for creating references of inherited beans
This commit is contained in:
Roland Praml
2017-02-26 12:25:55 +13:00
committed by Rob Bygrave
parent 7632910027
commit 40ca179509
3 changed files with 49 additions and 2 deletions
@@ -612,7 +612,7 @@ public final class DefaultServer implements SpiServer, SpiEbeanServer {
}
InheritInfo inheritInfo = desc.getInheritInfo();
if (inheritInfo == null) {
if (inheritInfo == null || inheritInfo.getChildren().isEmpty()) {
return (T) desc.contextRef(pc, null, false, id);
}
@@ -164,7 +164,14 @@ public class InheritInfo {
public BeanProperty[] localProperties() {
return descriptor.propertiesLocal();
}
/**
* Return the children.
*/
public ArrayList<InheritInfo> getChildren() {
return children;
}
/**
* Get the bean property additionally looking in the sub types.
*/
@@ -0,0 +1,40 @@
package org.tests.inheritance;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import org.avaje.ebeantest.LoggedSqlCollector;
import org.avaje.test.model.rawsql.inherit.ChildA;
import org.avaje.test.model.rawsql.inherit.ChildB;
import org.avaje.test.model.rawsql.inherit.Parent;
import org.junit.Test;
import io.ebean.BaseTestCase;
public class TestInheritanceRefBean extends BaseTestCase {
@Test
public void test() {
Parent a = new ChildA(42, "Bean A");
Parent b = new ChildB(43, "Bean B");
server().save(a);
server().save(b);
Long idA = a.getId();
Long idB = b.getId();
Parent test;
LoggedSqlCollector.start();
test = server().getReference(ChildA.class, idA);
assertTrue(test instanceof ChildA);
assertEquals(0, LoggedSqlCollector.stop().size());
LoggedSqlCollector.start();
test = server().getReference(Parent.class, idB);
assertTrue(test instanceof ChildB);
assertEquals(1, LoggedSqlCollector.stop().size());
}
}