From a0a98dfc5794b49ddc99ed6c05bd63c1996170e8 Mon Sep 17 00:00:00 2001 From: Komi Serge Innocent Date: Thu, 14 Nov 2013 15:37:33 +0000 Subject: [PATCH] DATAJPA-420 - Fixed count projection for manual queries with projections. Original pull request: #52. --- .../data/jpa/repository/query/QueryUtils.java | 3 ++- .../data/jpa/repository/query/QueryUtilsUnitTests.java | 9 +++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/springframework/data/jpa/repository/query/QueryUtils.java b/src/main/java/org/springframework/data/jpa/repository/query/QueryUtils.java index 761164b3f..e7f311e42 100644 --- a/src/main/java/org/springframework/data/jpa/repository/query/QueryUtils.java +++ b/src/main/java/org/springframework/data/jpa/repository/query/QueryUtils.java @@ -64,6 +64,7 @@ import org.springframework.util.StringUtils; * @author Oliver Gierke * @author Kevin Raymond * @author Thomas Darimont + * @author Komi Innocent */ public abstract class QueryUtils { @@ -341,7 +342,7 @@ public abstract class QueryUtils { Matcher matcher = COUNT_MATCH.matcher(originalQuery); String variable = matcher.matches() ? matcher.group(4) : null; boolean useVariable = StringUtils.hasText(variable) && !variable.startsWith("new") - && !variable.startsWith("count("); + && !variable.startsWith("count(") && !variable.contains(","); String countQuery = matcher.replaceFirst(String.format(COUNT_REPLACEMENT_TEMPLATE, useVariable ? SIMPLE_COUNT_VALUE : COMPLEX_COUNT_VALUE)); diff --git a/src/test/java/org/springframework/data/jpa/repository/query/QueryUtilsUnitTests.java b/src/test/java/org/springframework/data/jpa/repository/query/QueryUtilsUnitTests.java index 32c2cd0f5..977642210 100644 --- a/src/test/java/org/springframework/data/jpa/repository/query/QueryUtilsUnitTests.java +++ b/src/test/java/org/springframework/data/jpa/repository/query/QueryUtilsUnitTests.java @@ -30,6 +30,7 @@ import org.springframework.data.domain.Sort; * * @author Oliver Gierke * @author Thomas Darimont + * @author Komi Innocent */ public class QueryUtilsUnitTests { @@ -263,6 +264,14 @@ public class QueryUtilsUnitTests { assertCountQuery("select a.b from A a", "select count(a.b) from A a"); } + /** + * @see DATAJPA-420 + */ + @Test + public void createsCountQueryForScalarSelects() { + assertCountQuery("select p.lastname,p.firstname from Person p", "select count(p) from Person p"); + } + private void assertCountQuery(String originalQuery, String countQuery) { assertThat(createCountQueryFor(originalQuery), is(countQuery)); }