Skip to content

Commit 8924156

Browse files
committed
Ensure thatsingle-parameter DOTs work with JPQL queries.
Closes #1869.
1 parent b57eb85 commit 8924156

File tree

3 files changed

+30
-7
lines changed

3 files changed

+30
-7
lines changed

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

+5-4
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,8 @@ public abstract class QueryUtils {
114114

115115
private static final String EQUALS_CONDITION_STRING = "%s.%s = :%s";
116116
private static final Pattern ORDER_BY = Pattern.compile("(order\\s+by\\s+)", CASE_INSENSITIVE);
117-
private static final Pattern ORDER_BY_IN_WINDOW_OR_SUBSELECT = Pattern.compile("\\([\\s\\S]*order\\s+by\\s[\\s\\S]*\\)",
118-
CASE_INSENSITIVE);
117+
private static final Pattern ORDER_BY_IN_WINDOW_OR_SUBSELECT = Pattern
118+
.compile("\\([\\s\\S]*order\\s+by\\s[\\s\\S]*\\)", CASE_INSENSITIVE);
119119

120120
private static final Pattern NAMED_PARAMETER = Pattern.compile(COLON_NO_DOUBLE_COLON + IDENTIFIER + "|#" + IDENTIFIER,
121121
CASE_INSENSITIVE);
@@ -590,8 +590,9 @@ public static String createCountQueryFor(String originalQuery, @Nullable String
590590

591591
String variable = matcher.matches() ? matcher.group(VARIABLE_NAME_GROUP_INDEX) : null;
592592
boolean useVariable = StringUtils.hasText(variable) //
593-
&& !variable.startsWith(" new") //
594-
&& !variable.startsWith("count(") //
593+
&& !variable.startsWith("new") // select [new com.example.User...
594+
&& !variable.startsWith(" new") // select distinct[ new com.example.User...
595+
&& !variable.startsWith("count(") // select [count(...
595596
&& !variable.contains(",");
596597

597598
String complexCountValue = matcher.matches() && StringUtils.hasText(matcher.group(COMPLEX_COUNT_FIRST_INDEX))

spring-data-jpa/src/test/java/org/springframework/data/jpa/repository/UserRepositoryFinderTests.java

-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import org.junit.jupiter.api.BeforeEach;
2626
import org.junit.jupiter.api.Test;
2727
import org.junit.jupiter.api.extension.ExtendWith;
28-
2928
import org.springframework.beans.factory.annotation.Autowired;
3029
import org.springframework.dao.InvalidDataAccessApiUsageException;
3130
import org.springframework.data.domain.Page;

spring-data-jpa/src/test/java/org/springframework/data/jpa/repository/query/DefaultQueryUtilsUnitTests.java

+25-2
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,29 @@ void createsCountQueryForJoins() {
8484
"select count(distinct u) from User u left outer join u.roles r WHERE r = ?");
8585
}
8686

87+
@Test // GH-1869
88+
void createsCountQueryForJoinsWithTwoArgs() {
89+
90+
assertCountQuery("select distinct new User(u.name, u.age) from User u left outer join u.roles r WHERE r = ?",
91+
"select count(distinct u) from User u left outer join u.roles r WHERE r = ?");
92+
}
93+
94+
@Test // GH-1869
95+
void createsCountQueryForDtoWithOneArg() {
96+
97+
assertCountQuery(
98+
"SELECT new org.springframework.data.jpa.repository.sample.FirstNameDto(u.firstname) from User u where u.firstname = ?",
99+
"select count(u) from User u where u.firstname = ?");
100+
}
101+
102+
@Test // GH-1869
103+
void createsCountQueryForDtoWithTwoArgs() {
104+
105+
assertCountQuery(
106+
"SELECT new org.springframework.data.jpa.repository.sample.NameOnlyDto(u.firstname, u.lastname) from User u where u.firstname = ?",
107+
"select count(u) from User u where u.firstname = ?");
108+
}
109+
87110
@Test
88111
void createsCountQueryForQueriesWithSubSelects() {
89112

@@ -400,8 +423,8 @@ void doesNotContainStaticClauseInExistsQuery() {
400423
@Test // DATAJPA-1363
401424
void discoversAliasWithComplexFunction() {
402425

403-
assertThat(QueryUtils
404-
.getFunctionAliases("select new MyDto(sum(case when myEntity.prop3=0 then 1 else 0 end) as myAlias")) //
426+
assertThat(
427+
QueryUtils.getFunctionAliases("select new MyDto(sum(case when myEntity.prop3=0 then 1 else 0 end) as myAlias")) //
405428
.contains("myAlias");
406429
}
407430

0 commit comments

Comments
 (0)