Skip to content

Commit 21991ce

Browse files
committed
DATAJPA-938 - Fixed constructor expression detection in QueryUtils.
Slightly loosened our regular expression to detect constructor expressions as previously it didn't match select expressions that contained a distinct clause.
1 parent c228d3e commit 21991ce

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

src/main/java/org/springframework/data/jpa/repository/query/QueryUtils.java

+1
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ public abstract class QueryUtils {
135135
builder = new StringBuilder();
136136
builder.append("select");
137137
builder.append("\\s+"); // at least one space separating
138+
builder.append("(.*\\s+)?"); // anything in between (e.g. distinct) at least one space separating
138139
builder.append("new");
139140
builder.append("\\s+"); // at least one space separating
140141
builder.append(IDENTIFIER);

src/test/java/org/springframework/data/jpa/repository/query/QueryUtilsUnitTests.java

+9-1
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,15 @@ public void doesPrefixPropertyWith() {
323323
assertThat(applySorting(query, sort, "c"), endsWith("order by c.dPropertyStartingWithJoinAlias asc"));
324324
}
325325

326-
private void assertCountQuery(String originalQuery, String countQuery) {
326+
/**
327+
* @see DATAJPA-938
328+
*/
329+
@Test
330+
public void detectsConstructorExpressionInDistinctQuery() {
331+
assertThat(hasConstructorExpression("select distinct new Foo() from Bar b"), is(true));
332+
}
333+
334+
private static void assertCountQuery(String originalQuery, String countQuery) {
327335
assertThat(createCountQueryFor(originalQuery), is(countQuery));
328336
}
329337
}

0 commit comments

Comments
 (0)