Ensure that single-parameter DTOs work with JPQL queries.

Closes #1869.
This commit is contained in:
Greg L. Turnquist
2022-06-13 15:27:33 -05:00
parent b57eb85a23
commit f2e7bddb5a
3 changed files with 30 additions and 7 deletions

View File

@@ -114,8 +114,8 @@ public abstract class QueryUtils {
private static final String EQUALS_CONDITION_STRING = "%s.%s = :%s";
private static final Pattern ORDER_BY = Pattern.compile("(order\\s+by\\s+)", CASE_INSENSITIVE);
private static final Pattern ORDER_BY_IN_WINDOW_OR_SUBSELECT = Pattern.compile("\\([\\s\\S]*order\\s+by\\s[\\s\\S]*\\)",
CASE_INSENSITIVE);
private static final Pattern ORDER_BY_IN_WINDOW_OR_SUBSELECT = Pattern
.compile("\\([\\s\\S]*order\\s+by\\s[\\s\\S]*\\)", CASE_INSENSITIVE);
private static final Pattern NAMED_PARAMETER = Pattern.compile(COLON_NO_DOUBLE_COLON + IDENTIFIER + "|#" + IDENTIFIER,
CASE_INSENSITIVE);
@@ -590,8 +590,9 @@ public abstract class QueryUtils {
String variable = matcher.matches() ? matcher.group(VARIABLE_NAME_GROUP_INDEX) : null;
boolean useVariable = StringUtils.hasText(variable) //
&& !variable.startsWith(" new") //
&& !variable.startsWith("count(") //
&& !variable.startsWith("new") // select [new com.example.User...
&& !variable.startsWith(" new") // select distinct[ new com.example.User...
&& !variable.startsWith("count(") // select [count(...
&& !variable.contains(",");
String complexCountValue = matcher.matches() && StringUtils.hasText(matcher.group(COMPLEX_COUNT_FIRST_INDEX))

View File

@@ -25,7 +25,6 @@ import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.InvalidDataAccessApiUsageException;
import org.springframework.data.domain.Page;

View File

@@ -84,6 +84,29 @@ class DefaultQueryUtilsUnitTests {
"select count(distinct u) from User u left outer join u.roles r WHERE r = ?");
}
@Test // GH-1869
void createsCountQueryForJoinsWithTwoArgs() {
assertCountQuery("select distinct new User(u.name, u.age) from User u left outer join u.roles r WHERE r = ?",
"select count(distinct u) from User u left outer join u.roles r WHERE r = ?");
}
@Test // GH-1869
void createsCountQueryForDtoWithOneArg() {
assertCountQuery(
"SELECT new org.springframework.data.jpa.repository.sample.FirstNameDto(u.firstname) from User u where u.firstname = ?",
"select count(u) from User u where u.firstname = ?");
}
@Test // GH-1869
void createsCountQueryForDtoWithTwoArgs() {
assertCountQuery(
"SELECT new org.springframework.data.jpa.repository.sample.NameOnlyDto(u.firstname, u.lastname) from User u where u.firstname = ?",
"select count(u) from User u where u.firstname = ?");
}
@Test
void createsCountQueryForQueriesWithSubSelects() {
@@ -400,8 +423,8 @@ class DefaultQueryUtilsUnitTests {
@Test // DATAJPA-1363
void discoversAliasWithComplexFunction() {
assertThat(QueryUtils
.getFunctionAliases("select new MyDto(sum(case when myEntity.prop3=0 then 1 else 0 end) as myAlias")) //
assertThat(
QueryUtils.getFunctionAliases("select new MyDto(sum(case when myEntity.prop3=0 then 1 else 0 end) as myAlias")) //
.contains("myAlias");
}