Skip to content

Commit e595ea5

Browse files
authored
Fix missing order by clause for native queries (#3853)
1 parent ce05e65 commit e595ea5

File tree

2 files changed

+29
-7
lines changed

2 files changed

+29
-7
lines changed

modules/flowable-engine-common/src/main/java/org/flowable/common/engine/impl/query/BaseNativeQuery.java

+5-7
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,13 @@ protected Map<String, Object> generateParameterMap() {
5656
parameterMap.put("needsPaging", firstResult >= 0);
5757
String orderBy = (String) parameterMap.get("orderBy");
5858
if (orderBy != null && !"".equals(orderBy)) {
59-
String columns = "RES." + orderBy;
60-
parameterMap.put("orderBy", columns);
61-
parameterMap.put("orderByColumns", columns);
62-
parameterMap.put("orderByForWindow", columns);
59+
orderBy = "RES." + orderBy;
6360
} else {
64-
parameterMap.put("orderBy", "order by RES.ID_ asc");
65-
parameterMap.put("orderByForWindow", "order by RES.ID_ asc");
66-
parameterMap.put("orderByColumns", "RES.ID_ asc");
61+
orderBy = "RES.ID_ asc";
6762
}
63+
parameterMap.put("orderBy", "order by " + orderBy);
64+
parameterMap.put("orderByForWindow", "order by " + orderBy);
65+
parameterMap.put("orderByColumns", orderBy);
6866

6967
int firstRow = firstResult + 1;
7068
parameterMap.put("firstRow", firstRow);

modules/flowable-engine/src/test/java/org/flowable/engine/test/api/identity/GroupQueryTest.java

+24
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import org.flowable.common.engine.api.FlowableException;
2222
import org.flowable.common.engine.api.FlowableIllegalArgumentException;
23+
import org.flowable.common.engine.impl.AbstractEngineConfiguration;
2324
import org.flowable.engine.impl.test.PluggableFlowableTestCase;
2425
import org.flowable.idm.api.Group;
2526
import org.flowable.idm.api.GroupQuery;
@@ -258,4 +259,27 @@ public void testNativeQuery() {
258259
assertThat(identityService.createNativeGroupQuery().sql(baseQuerySql + " where ID_ = #{id}").parameter("id", "admin").listPage(0, 1)).hasSize(1);
259260
}
260261

262+
@Test
263+
public void testNativeQueryOrder() {
264+
StringBuilder baseQueryBuilder = new StringBuilder();
265+
baseQueryBuilder.append("SELECT ");
266+
if (AbstractEngineConfiguration.DATABASE_TYPE_MSSQL.equals(processEngineConfiguration.getDatabaseType())) {
267+
baseQueryBuilder.append("top 100 percent ");
268+
}
269+
baseQueryBuilder.append("* FROM ")
270+
.append(IdentityTestUtil.getTableName("ACT_ID_GROUP", processEngineConfiguration))
271+
.append(" order by ID_ desc");
272+
String baseQuerySql = baseQueryBuilder.toString();
273+
274+
assertThat(identityService.createNativeGroupQuery().sql(baseQuerySql).list())
275+
.extracting(Group::getId)
276+
.containsExactly("muppets", "mammals", "frogs", "admin");
277+
278+
assertThat(identityService.createNativeGroupQuery().sql(baseQuerySql)
279+
.parameter("orderBy", "ID_ desc")
280+
.listPage(0, 2))
281+
.extracting(Group::getId)
282+
.containsExactly("muppets", "mammals");
283+
}
284+
261285
}

0 commit comments

Comments
 (0)