From 19eca5f9e028aa5382199a3ab689eb2c618ecdb2 Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Wed, 28 Sep 2016 15:06:27 +0200 Subject: [PATCH] DATAJPA-974 - Query for tuples now uses joins correctly. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When creating the selections for a derived query using a projection we now explicitly create joins for plural attributes. Looks like Hibernate fails to create a proper query if the path is referred to via root.get(…) and the select clause includes references to other non-plural attributes. --- .../data/jpa/repository/query/JpaQueryCreator.java | 4 +++- .../data/jpa/repository/UserRepositoryFinderTests.java | 8 ++++++++ .../data/jpa/repository/sample/UserRepository.java | 9 +++++++++ 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/springframework/data/jpa/repository/query/JpaQueryCreator.java b/src/main/java/org/springframework/data/jpa/repository/query/JpaQueryCreator.java index e2ef5f5cb..6b64b85d8 100644 --- a/src/main/java/org/springframework/data/jpa/repository/query/JpaQueryCreator.java +++ b/src/main/java/org/springframework/data/jpa/repository/query/JpaQueryCreator.java @@ -156,7 +156,9 @@ public class JpaQueryCreator extends AbstractQueryCreator> selections = new ArrayList>(); for (String property : returnedType.getInputProperties()) { - selections.add(root.get(property).alias(property)); + + PropertyPath path = PropertyPath.from(property, returnedType.getDomainType()); + selections.add(toExpressionRecursively(root, path).alias(property)); } query = query.multiselect(selections); diff --git a/src/test/java/org/springframework/data/jpa/repository/UserRepositoryFinderTests.java b/src/test/java/org/springframework/data/jpa/repository/UserRepositoryFinderTests.java index 9422e465e..5d34458f8 100644 --- a/src/test/java/org/springframework/data/jpa/repository/UserRepositoryFinderTests.java +++ b/src/test/java/org/springframework/data/jpa/repository/UserRepositoryFinderTests.java @@ -226,4 +226,12 @@ public class UserRepositoryFinderTests { public void translatesNotContainsToNotMemberOf() { assertThat(userRepository.findByRolesNotContaining(drummer), hasItems(dave, oliver)); } + + /** + * @see DATAJPA-974 + */ + @Test + public void executesQueryWithProjectionContainingReferenceToPluralAttribute() { + assertThat(userRepository.findRolesAndFirstnameBy(), is(notNullValue())); + } } diff --git a/src/test/java/org/springframework/data/jpa/repository/sample/UserRepository.java b/src/test/java/org/springframework/data/jpa/repository/sample/UserRepository.java index d1ee91e8a..b01d19ee1 100644 --- a/src/test/java/org/springframework/data/jpa/repository/sample/UserRepository.java +++ b/src/test/java/org/springframework/data/jpa/repository/sample/UserRepository.java @@ -596,4 +596,13 @@ public interface UserRepository * @see DATAJPA-858 */ List findByRolesNameContaining(String name); + + List findRolesAndFirstnameBy(); + + static interface RolesAndFirstname { + + String getFirstname(); + + Set getRoles(); + } }