Skip to content

Commit 19eca5f

Browse files
committed
DATAJPA-974 - Query for tuples now uses joins correctly.
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.
1 parent e5b3984 commit 19eca5f

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

src/main/java/org/springframework/data/jpa/repository/query/JpaQueryCreator.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,9 @@ protected CriteriaQuery<? extends Object> complete(Predicate predicate, Sort sor
156156
List<Selection<?>> selections = new ArrayList<Selection<?>>();
157157

158158
for (String property : returnedType.getInputProperties()) {
159-
selections.add(root.get(property).alias(property));
159+
160+
PropertyPath path = PropertyPath.from(property, returnedType.getDomainType());
161+
selections.add(toExpressionRecursively(root, path).alias(property));
160162
}
161163

162164
query = query.multiselect(selections);

src/test/java/org/springframework/data/jpa/repository/UserRepositoryFinderTests.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,4 +226,12 @@ public void translatesContainsToMemberOf() {
226226
public void translatesNotContainsToNotMemberOf() {
227227
assertThat(userRepository.findByRolesNotContaining(drummer), hasItems(dave, oliver));
228228
}
229+
230+
/**
231+
* @see DATAJPA-974
232+
*/
233+
@Test
234+
public void executesQueryWithProjectionContainingReferenceToPluralAttribute() {
235+
assertThat(userRepository.findRolesAndFirstnameBy(), is(notNullValue()));
236+
}
229237
}

src/test/java/org/springframework/data/jpa/repository/sample/UserRepository.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -596,4 +596,13 @@ List<User> findUsersByFirstnameForSpELExpressionWithParameterIndexOnlyWithEntity
596596
* @see DATAJPA-858
597597
*/
598598
List<User> findByRolesNameContaining(String name);
599+
600+
List<RolesAndFirstname> findRolesAndFirstnameBy();
601+
602+
static interface RolesAndFirstname {
603+
604+
String getFirstname();
605+
606+
Set<Role> getRoles();
607+
}
599608
}

0 commit comments

Comments
 (0)