Skip to content

Commit

Permalink
FIX ebean-orm#2773 Fix wrong query in combination with fetch & where
Browse files Browse the repository at this point in the history
  • Loading branch information
rPraml committed Aug 3, 2023
1 parent c502156 commit 31c67c0
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -673,11 +673,6 @@ private SqlTreeNodeExtraJoin findExtraJoinRoot(String includeProp, SqlTreeNodeEx
} else {
// look in register ...
String parentPropertyName = includeProp.substring(0, dotPos);
if (selectIncludes.contains(parentPropertyName)) {
// parent already handled by select
return childJoin;
}

SqlTreeNodeExtraJoin parentJoin = joinRegister.get(parentPropertyName);
if (parentJoin == null) {
// we need to create this the parent implicitly...
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -378,4 +378,80 @@ public void test_softRefChildren() {
+ "left join e_basic u2 on u2.id = u1_ref.id where u2.id is null");

}

@Test
public void test_fetch_only() {

LoggedSql.start();

DB.find(ChildPerson.class).select("name").fetch("parent.effectiveBean").findList();

List<String> loggedSql = LoggedSql.stop();
assertThat(loggedSql.get(0)).contains("from child_person t0 "
+ "left join parent_person t1 on t1.identifier = t0.parent_identifier "
+ "left join grand_parent_person j1 on j1.identifier = t1.parent_identifier "
+ "left join e_basic t2 on t2.id = coalesce(t1.some_bean_id, j1.some_bean_id)");
}

@Test
public void test_where_only() {

LoggedSql.start();

DB.find(ChildPerson.class).select("name")
.where().eq("parent.effectiveBean.name", "foo")
.findList();

List<String> loggedSql = LoggedSql.stop();
assertThat(loggedSql.get(0))
.contains("from child_person t0 "
+ "left join parent_person t1 on t1.identifier = t0.parent_identifier "
+ "left join grand_parent_person j1 on j1.identifier = t1.parent_identifier "
+ "left join e_basic t2 on t2.id = coalesce(t1.some_bean_id, j1.some_bean_id) "
+ "where t2.name = ?");
}

@Test
public void test_fetch_one_prop_with_where() {

LoggedSql.start();
// #2773: Ebean produces a wrong join, when one half comes from the fetch
// and the other half comes from the where
// fetching both properties or none will produce a correct query
DB.find(ChildPerson.class).select("name")
.fetch("parent", "name")
//.fetch("parent.effectiveBean", "name")
.where().eq("parent.effectiveBean.name", "foo")
.findList();

List<String> loggedSql = LoggedSql.stop();
assertThat(loggedSql.get(0))
.contains("from child_person t0 "
+ "left join parent_person t1 on t1.identifier = t0.parent_identifier "
+ "left join grand_parent_person j1 on j1.identifier = t1.parent_identifier "
+ "left join e_basic t2 on t2.id = coalesce(t1.some_bean_id, j1.some_bean_id)"
+ " where t2.name = ?"
);
}

@Test
public void test_fetch_complete_with_where() {

LoggedSql.start();

DB.find(ChildPerson.class).select("name")
.fetch("parent")
.where().eq("parent.effectiveBean.name", "foo")
.findList();

List<String> loggedSql = LoggedSql.stop();
assertThat(loggedSql.get(0))
.contains("from child_person t0 "
+ "left join parent_person t1 on t1.identifier = t0.parent_identifier "
+ "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) f2 on f2.parent_identifier = t1.identifier "
+ "left join grand_parent_person j1 on j1.identifier = t1.parent_identifier "
+ "left join e_basic t2 on t2.id = coalesce(t1.some_bean_id, j1.some_bean_id) "
+ "where t2.name = ?");
}

}

0 comments on commit 31c67c0

Please sign in to comment.