Skip to content

Commit eeeb188

Browse files
committed
DATAJDBC-560 - Fix Criteria mapping when composing a group from top-level criteria.
Using Criteria.from(…) with multiple Criteria objects now uses properly AND combination along with group nesting to render a correct criteria. Previously, the INITIAL combinator in groups caused a mapping exception.
1 parent 81aba29 commit eeeb188

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/query/QueryMapper.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ private Condition unroll(CriteriaDefinition criteria, Table table, @Nullable Rel
191191

192192
Condition condition = getCondition(criterion, parameterSource, table, entity);
193193
if (condition != null) {
194-
result = combine(criterion, mapped, criterion.getCombinator(), condition);
194+
result = combine(mapped, criterion.getCombinator(), condition);
195195
}
196196

197197
if (result != null) {
@@ -221,7 +221,7 @@ private Condition unrollGroup(List<? extends CriteriaDefinition> criteria, Table
221221

222222
Condition condition = unroll(criterion, table, entity, parameterSource);
223223

224-
mapped = combine(criterion, mapped, combinator, condition);
224+
mapped = combine(mapped, combinator, condition);
225225
}
226226

227227
return mapped;
@@ -245,17 +245,19 @@ private Condition getCondition(CriteriaDefinition criteria, MapSqlParameterSourc
245245
return mapCondition(criteria, parameterSource, table, entity);
246246
}
247247

248-
private Condition combine(CriteriaDefinition criteria, @Nullable Condition currentCondition,
248+
private Condition combine(@Nullable Condition currentCondition,
249249
CriteriaDefinition.Combinator combinator, Condition nextCondition) {
250250

251251
if (currentCondition == null) {
252252
currentCondition = nextCondition;
253+
} else if (combinator == CriteriaDefinition.Combinator.INITIAL) {
254+
currentCondition = currentCondition.and(Conditions.nest(nextCondition));
253255
} else if (combinator == CriteriaDefinition.Combinator.AND) {
254256
currentCondition = currentCondition.and(nextCondition);
255257
} else if (combinator == CriteriaDefinition.Combinator.OR) {
256258
currentCondition = currentCondition.or(nextCondition);
257259
} else {
258-
throw new IllegalStateException("Combinator " + criteria.getCombinator() + " not supported");
260+
throw new IllegalStateException("Combinator " + combinator + " not supported");
259261
}
260262

261263
return currentCondition;

spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/query/QueryMapperUnitTests.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,20 @@ public void shouldMapFrom() {
126126
.hasToString("person.\"NAME\" = ?[:name] AND (person.\"NAME\" = ?[:name1] OR person.age < ?[:age])");
127127
}
128128

129+
@Test // DATAJDBC-560
130+
public void shouldMapFromConcat() {
131+
132+
Criteria criteria = Criteria.from(Criteria.where("name").is("Foo"), Criteria.where("name").is("Bar") //
133+
.or("age").lessThan(49));
134+
135+
assertThat(criteria.isEmpty()).isFalse();
136+
137+
Condition condition = map(criteria);
138+
139+
assertThat(condition)
140+
.hasToString("(person.\"NAME\" = ?[:name] AND (person.\"NAME\" = ?[:name1] OR person.age < ?[:age]))");
141+
}
142+
129143
@Test // DATAJDBC-318
130144
public void shouldMapSimpleCriteria() {
131145

0 commit comments

Comments
 (0)