Skip to content

Commit 593d73a

Browse files
committed
Fix SQL generation for query with sorting.
Closes #1919
1 parent 73e5033 commit 593d73a

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/convert/SqlGenerator.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1022,7 +1022,7 @@ private SelectBuilder.SelectOrdered applyQueryOnSelect(Query query, MapSqlParame
10221022

10231023
if (query.isSorted()) {
10241024
List<OrderByField> sort = this.queryMapper.getMappedSort(table, query.getSort(), entity);
1025-
selectOrdered = selectBuilder.orderBy(sort);
1025+
selectOrdered = selectOrdered.orderBy(sort);
10261026
}
10271027

10281028
SelectBuilder.SelectLimitOffset limitable = (SelectBuilder.SelectLimitOffset) selectOrdered;

spring-data-jdbc/src/test/java/org/springframework/data/jdbc/core/convert/SqlGeneratorUnitTests.java

+36
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,42 @@ void findAllPagedAndSorted() {
342342
"LIMIT 10");
343343
}
344344

345+
@Test // GH-1919
346+
void selectByQuery() {
347+
348+
Query query = Query.query(Criteria.where("id").is(23L));
349+
350+
String sql = sqlGenerator.selectByQuery(query, new MapSqlParameterSource());
351+
352+
assertThat(sql).contains( //
353+
"SELECT", //
354+
"FROM dummy_entity", //
355+
"LEFT OUTER JOIN referenced_entity ref ON ref.dummy_entity = dummy_entity.id1", //
356+
"LEFT OUTER JOIN second_level_referenced_entity ref_further ON ref_further.referenced_entity = ref.x_l1id", //
357+
"WHERE dummy_entity.id1 = :id1" //
358+
);
359+
}
360+
361+
@Test // GH-1919
362+
void selectBySortedQuery() {
363+
364+
Query query = Query.query(Criteria.where("id").is(23L)) //
365+
.sort(Sort.by(Sort.Order.asc("id")));
366+
367+
String sql = sqlGenerator.selectByQuery(query, new MapSqlParameterSource());
368+
369+
assertThat(sql).contains( //
370+
"SELECT", //
371+
"FROM dummy_entity", //
372+
"LEFT OUTER JOIN referenced_entity ref ON ref.dummy_entity = dummy_entity.id1", //
373+
"LEFT OUTER JOIN second_level_referenced_entity ref_further ON ref_further.referenced_entity = ref.x_l1id", //
374+
"WHERE dummy_entity.id1 = :id1", //
375+
"ORDER BY dummy_entity.id1 ASC" //
376+
);
377+
assertThat(sql).containsOnlyOnce("LEFT OUTER JOIN referenced_entity ref ON ref.dummy_entity = dummy_entity.id1");
378+
assertThat(sql).containsOnlyOnce("LEFT OUTER JOIN second_level_referenced_entity ref_further ON ref_further.referenced_entity = ref.x_l1id");
379+
}
380+
345381
@Test // DATAJDBC-131, DATAJDBC-111
346382
void findAllByProperty() {
347383

0 commit comments

Comments
 (0)