mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
FIX: @Formula on @ManyToOne produces SQLException ... fix appends missing FROM clause for formula joins #1299
This commit is contained in:
committed by
Rob Bygrave
parent
11e180aa3f
commit
021d57f4b2
@@ -219,4 +219,46 @@ public class TestInheritance extends BaseTestCase {
|
||||
assertEquals("Munich", grandparent1.getAddress());
|
||||
assertEquals(1, grandparent1.getEffectiveBean().getId().intValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFindCount() {
|
||||
EBasic basicA = new EBasic();
|
||||
basicA.setName("FamilyName A");
|
||||
basicA.setDescription("Description A");
|
||||
server().save(basicA);
|
||||
|
||||
EBasic basicB = new EBasic();
|
||||
basicB.setName("FamilyName B");
|
||||
basicB.setDescription("Description B");
|
||||
server().save(basicB);
|
||||
|
||||
|
||||
GrandParentPerson gp1 = new GrandParentPerson();
|
||||
gp1.setFamilyName("FamilyName A");
|
||||
gp1.setName("Franz");
|
||||
server().save(gp1);
|
||||
|
||||
gp1 = server().find(GrandParentPerson.class).where().eq("name", "Franz").findOne();
|
||||
assertEquals("FamilyName A", gp1.getFamilyName());
|
||||
assertEquals("Description A", gp1.getBasicSameName().getDescription());
|
||||
|
||||
|
||||
gp1 = server().find(GrandParentPerson.class).where().eq("basicSameName.name", "FamilyName A").findOne();
|
||||
assertEquals("FamilyName A", gp1.getFamilyName());
|
||||
assertEquals("Description A", gp1.getBasicSameName().getDescription());
|
||||
|
||||
|
||||
gp1 = server().find(GrandParentPerson.class).where().eq("basicSameName.description", "Description A").findOne();
|
||||
assertEquals("FamilyName A", gp1.getFamilyName());
|
||||
assertEquals("Description A", gp1.getBasicSameName().getDescription());
|
||||
|
||||
int count;
|
||||
count = server().find(GrandParentPerson.class).where().eq("name", "Franz").findCount();
|
||||
assertEquals(1, count);
|
||||
count = server().find(GrandParentPerson.class).where().eq("basicSameName.name", "FamilyName A").findCount();
|
||||
assertEquals(1, count);
|
||||
count = server().find(GrandParentPerson.class).where().eq("basicSameName.description", "Description A").findCount();
|
||||
assertEquals(1, count);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ import org.tests.model.basic.EBasic;
|
||||
|
||||
import javax.persistence.CascadeType;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.FetchType;
|
||||
import javax.persistence.ManyToOne;
|
||||
import javax.persistence.OneToMany;
|
||||
import javax.persistence.OrderBy;
|
||||
@@ -22,7 +23,7 @@ public class GrandParentPerson extends InheritablePerson {
|
||||
// This rather complex formulas should be built later by CustomAnnotationParser
|
||||
private static final String PARENT_PERSON_AGGREGATE_JOIN = "left join "
|
||||
+ "(select i1.parent_identifier, count(*) as child_count, sum(i1.age) as child_age from parent_person i1 group by i1.parent_identifier) "
|
||||
+ "as f1 on f1.parent_identifier = ${ta}.identifier";
|
||||
+ "f1 on f1.parent_identifier = ${ta}.identifier";
|
||||
|
||||
//@Count("children")
|
||||
@Formula(select = "coalesce(f1.child_count, 0)", join = PARENT_PERSON_AGGREGATE_JOIN)
|
||||
@@ -37,6 +38,11 @@ public class GrandParentPerson extends InheritablePerson {
|
||||
private String address;
|
||||
|
||||
|
||||
@ManyToOne(optional = true, fetch = FetchType.EAGER)
|
||||
@Formula(select = "f3.id", join = "left join e_basic f3 on f3.name = ${ta}.family_name")
|
||||
private EBasic basicSameName;
|
||||
|
||||
|
||||
// Demonstrate formula usage
|
||||
@Formula(select = "coalesce(${ta}.some_bean_id,1)")
|
||||
@ManyToOne
|
||||
@@ -78,4 +84,12 @@ public class GrandParentPerson extends InheritablePerson {
|
||||
public EBasic getEffectiveBean() {
|
||||
return effectiveBean;
|
||||
}
|
||||
|
||||
public EBasic getBasicSameName() {
|
||||
return basicSameName;
|
||||
}
|
||||
|
||||
public void setBasicSameName(EBasic basicSameName) {
|
||||
this.basicSameName = basicSameName;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,7 +19,7 @@ public class ParentPerson extends InheritablePerson {
|
||||
//This rather complex formulas should be built later by CustomAnnotationParser
|
||||
private static final String CHILD_PERSON_AGGREGATE_JOIN = "left join "
|
||||
+ "(select i2.parent_identifier, count(*) as child_count, sum(i2.age) as child_age from child_person i2 group by i2.parent_identifier) "
|
||||
+ "as f2 on f2.parent_identifier = ${ta}.identifier";
|
||||
+ "f2 on f2.parent_identifier = ${ta}.identifier";
|
||||
|
||||
private static final String GRAND_PARENT_PERSON_JOIN = "join grand_parent_person j1 on j1.identifier = ${ta}.parent_identifier";
|
||||
|
||||
|
||||
Reference in New Issue
Block a user