Skip to content

Commit 591f260

Browse files
authored
Retain filterMany expression when select fetchGroup applied after filterMany (#3673)
1 parent b361b10 commit 591f260

File tree

6 files changed

+34
-7
lines changed

6 files changed

+34
-7
lines changed

ebean-core/src/main/java/io/ebeaninternal/server/query/DFetchGroup.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ final class DFetchGroup<T> implements SpiFetchGroup<T> {
1515
}
1616

1717
@Override
18-
public OrmQueryDetail detail() {
19-
return detail.copy();
18+
public OrmQueryDetail detail(OrmQueryDetail existing) {
19+
return detail.copy(existing);
2020
}
2121

2222
@Override

ebean-core/src/main/java/io/ebeaninternal/server/query/DefaultFetchGroupQuery.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public Query<T> select(String columns) {
5252
@SuppressWarnings("rawtypes")
5353
@Override
5454
public Query<T> select(FetchGroup fetchGroup) {
55-
this.detail = ((SpiFetchGroup) fetchGroup).detail();
55+
this.detail = ((SpiFetchGroup) fetchGroup).detail(detail);
5656
return this;
5757
}
5858

ebean-core/src/main/java/io/ebeaninternal/server/querydefn/DefaultOrmQuery.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -769,7 +769,7 @@ public SpiQuery<T> copy(SpiEbeanServer server) {
769769
copy.useQueryCache = useQueryCache;
770770
copy.unmodifiable = unmodifiable;
771771
if (detail != null) {
772-
copy.detail = detail.copy();
772+
copy.detail = detail.copy(null);
773773
}
774774
copy.temporalMode = temporalMode;
775775
copy.firstRow = firstRow;
@@ -1387,7 +1387,7 @@ public final Query<T> select(String columns) {
13871387
@Override
13881388
public final Query<T> select(FetchGroup<T> fetchGroup) {
13891389
if (fetchGroup != null) {
1390-
this.detail = ((SpiFetchGroup<T>) fetchGroup).detail();
1390+
this.detail = ((SpiFetchGroup<T>) fetchGroup).detail(detail);
13911391
}
13921392
return this;
13931393
}

ebean-core/src/main/java/io/ebeaninternal/server/querydefn/OrmQueryDetail.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,21 @@ public final class OrmQueryDetail implements Serializable {
3939
/**
4040
* Return a deep copy of the OrmQueryDetail.
4141
*/
42-
public OrmQueryDetail copy() {
42+
public OrmQueryDetail copy(OrmQueryDetail existing) {
4343
OrmQueryDetail copy = new OrmQueryDetail();
4444
copy.baseProps = baseProps.copy();
4545
for (Map.Entry<String, OrmQueryProperties> entry : fetchPaths.entrySet()) {
4646
copy.fetchPaths.put(entry.getKey(), entry.getValue().copy());
4747
}
48+
if (existing != null) {
49+
// transfer any existing filterMany expressions
50+
for (Map.Entry<String, OrmQueryProperties> entry : existing.fetchPaths.entrySet()) {
51+
var filterMany = entry.getValue().getFilterMany();
52+
if (filterMany != null) {
53+
copy.getChunk(entry.getKey(), true).setFilterMany(filterMany);
54+
}
55+
}
56+
}
4857
return copy;
4958
}
5059

ebean-core/src/main/java/io/ebeaninternal/server/querydefn/SpiFetchGroup.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public interface SpiFetchGroup<T> extends FetchGroup<T> {
1010
/**
1111
* Return the detail to use for query execution.
1212
*/
13-
OrmQueryDetail detail();
13+
OrmQueryDetail detail(OrmQueryDetail existing);
1414

1515
/**
1616
* Return the underlying detail for copy purposes.

ebean-querybean/src/test/java/org/querytest/QCustomerTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import org.example.domain.query.QContact;
1414
import org.example.domain.query.QCustomer;
1515
import org.junit.jupiter.api.Disabled;
16+
import org.junit.jupiter.api.DisplayName;
1617
import org.junit.jupiter.api.Test;
1718

1819
import javax.sql.DataSource;
@@ -391,6 +392,23 @@ void filterManyOr() {
391392
assertThat(q.getGeneratedSql()).contains(" from be_customer t0 left join be_contact t1 on t1.customer_id = t0.id where (t1.id is null or ((t1.first_name like ? escape'|' or t1.last_name like ? escape'|'))) order by t0.id");
392393
}
393394

395+
@Test
396+
@DisplayName("Retain filterMany expression when select fetchGroup applied after filterMany")
397+
void filterManyBeforeSelectFetchGroup_expect_filterManyExpressionRetained() {
398+
var fetchGroup = QCustomer.forFetchGroup()
399+
.select(name)
400+
.contacts.fetch(QContact.Alias.email)
401+
.buildFetchGroup();
402+
403+
var q = new QCustomer()
404+
.contacts.filterMany(c -> c.firstName.startsWith("R"))
405+
.select(fetchGroup)
406+
.query();
407+
408+
q.findList();
409+
assertThat(q.getGeneratedSql()).contains(" t0.id, t0.name, t1.id, t1.email from be_customer t0 left join be_contact t1 on t1.customer_id = t0.id where (t1.id is null or (t1.first_name like ? escape'|')) order by t0.id");
410+
}
411+
394412
@Test
395413
public void testIdIn() {
396414

0 commit comments

Comments
 (0)