Skip to content

Commit d357839

Browse files
committed
Ensure that single-parameter DTOs work with JPQL queries.
Closes #1869.
1 parent f8737e3 commit d357839

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

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);
@@ -591,8 +591,9 @@ public static String createCountQueryFor(String originalQuery, @Nullable String
591591

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

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

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

+23
Original file line numberDiff line numberDiff line change
@@ -736,6 +736,29 @@ void countQueryUsesCorrectVariable() {
736736
.isEqualTo("select count(us) FROM users_statuses us WHERE (user_created_at BETWEEN :fromDate AND :toDate)");
737737
}
738738

739+
@Test // GH-1869
740+
void createsCountQueryForJoinsWithTwoArgs() {
741+
742+
assertCountQuery("select distinct new User(u.name, u.age) from User u left outer join u.roles r WHERE r = ?",
743+
"select count(distinct u) from User u left outer join u.roles r WHERE r = ?");
744+
}
745+
746+
@Test // GH-1869
747+
void createsCountQueryForDtoWithOneArg() {
748+
749+
assertCountQuery(
750+
"SELECT new org.springframework.data.jpa.repository.sample.FirstNameDto(u.firstname) from User u where u.firstname = ?",
751+
"select count(u) from User u where u.firstname = ?");
752+
}
753+
754+
@Test // GH-1869
755+
void createsCountQueryForDtoWithTwoArgs() {
756+
757+
assertCountQuery(
758+
"SELECT new org.springframework.data.jpa.repository.sample.NameOnlyDto(u.firstname, u.lastname) from User u where u.firstname = ?",
759+
"select count(u) from User u where u.firstname = ?");
760+
}
761+
739762
@Test // GH-2496, GH-2522, GH-2537, GH-2045
740763
void orderByShouldWorkWithSubSelectStatements() {
741764

0 commit comments

Comments
 (0)