diff --git a/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/QueryUtils.java b/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/QueryUtils.java index bd39edc50b..94c080dec6 100644 --- a/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/QueryUtils.java +++ b/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/QueryUtils.java @@ -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 static String createCountQueryFor(String originalQuery, @Nullable String 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)) diff --git a/spring-data-jpa/src/test/java/org/springframework/data/jpa/repository/UserRepositoryFinderTests.java b/spring-data-jpa/src/test/java/org/springframework/data/jpa/repository/UserRepositoryFinderTests.java index ff2c484004..6d89b1def3 100644 --- a/spring-data-jpa/src/test/java/org/springframework/data/jpa/repository/UserRepositoryFinderTests.java +++ b/spring-data-jpa/src/test/java/org/springframework/data/jpa/repository/UserRepositoryFinderTests.java @@ -25,7 +25,6 @@ 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; diff --git a/spring-data-jpa/src/test/java/org/springframework/data/jpa/repository/query/DefaultQueryUtilsUnitTests.java b/spring-data-jpa/src/test/java/org/springframework/data/jpa/repository/query/DefaultQueryUtilsUnitTests.java index ec08e3ea9d..828f8a835b 100644 --- a/spring-data-jpa/src/test/java/org/springframework/data/jpa/repository/query/DefaultQueryUtilsUnitTests.java +++ b/spring-data-jpa/src/test/java/org/springframework/data/jpa/repository/query/DefaultQueryUtilsUnitTests.java @@ -84,6 +84,29 @@ void createsCountQueryForJoins() { "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 @@ void doesNotContainStaticClauseInExistsQuery() { @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"); }