From 28de27107d7cfab44b20529696652d269c8f6a96 Mon Sep 17 00:00:00 2001 From: "Greg L. Turnquist" Date: Wed, 31 May 2023 15:52:16 -0500 Subject: [PATCH] Verify that projections work with subqueries. Interface-based projections used to NOT work when the query had a subquery in it. But with the new query parser, it already handles it. So this simply captures the test providing that corner case. See #2008 Original Pull Request: #420 --- .../jpa/repository/UserRepositoryFinderTests.java | 11 +++++++++++ .../data/jpa/repository/sample/UserRepository.java | 4 ++++ 2 files changed, 15 insertions(+) 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 4b6c4bb9e..5ded8dd6b 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 @@ -315,4 +315,15 @@ class UserRepositoryFinderTests { void executesNamedQueryWithConstructorExpression() { userRepository.findByNamedQueryWithConstructorExpression(); } + + @Test // DATAJPA-1713, GH-2008 + public void selectProjectionWithSubselect() { + + List dtos = userRepository.findProjectionBySubselect(); + + assertThat(dtos).flatExtracting(UserRepository.NameOnly::getFirstname) // + .containsExactly("Dave", "Carter", "Oliver August"); + assertThat(dtos).flatExtracting(UserRepository.NameOnly::getLastname) // + .containsExactly("Matthews", "Beauford", "Matthews"); + } } diff --git a/spring-data-jpa/src/test/java/org/springframework/data/jpa/repository/sample/UserRepository.java b/spring-data-jpa/src/test/java/org/springframework/data/jpa/repository/sample/UserRepository.java index 821b119a7..a1bd08711 100644 --- a/spring-data-jpa/src/test/java/org/springframework/data/jpa/repository/sample/UserRepository.java +++ b/spring-data-jpa/src/test/java/org/springframework/data/jpa/repository/sample/UserRepository.java @@ -718,6 +718,10 @@ public interface UserRepository extends JpaRepository, JpaSpecifi nativeQuery = true) int mergeNativeStatement(); + // DATAJPA-1713, GH-2008 + @Query("select u from User u where u.firstname >= (select Min(u0.firstname) from User u0)") + List findProjectionBySubselect(); + interface RolesAndFirstname { String getFirstname();