Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: fetch and where with formula join will produce incorrect joins #2773

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -677,11 +677,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 @@ -29,7 +29,7 @@ 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)")
@Formula(select = "coalesce(${ta}.some_bean_id, j1.some_bean_id, j2.some_bean_id)", join = PARENTS_JOIN)
@ManyToOne
private EBasic effectiveBean;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,79 @@ public void test_ChildPersonParentFindCount() {
assertThat(loggedSql.get(0)).contains("where coalesce(f2.child_age, 0) = ?");
}

@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();

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 = ?");
}

@Test
public void test_softRef() {

Expand Down