From 70d9beacb3f03c78d4c470cf9b2c619309dd09f7 Mon Sep 17 00:00:00 2001 From: "Greg L. Turnquist" Date: Wed, 26 Jul 2023 16:50:46 -0500 Subject: [PATCH] Verify that mismatched return types are caught. Verify that when a return type doesn't match the query's type, an exception is thrown. See #1184 --- ...WithNullLikeHibernateIntegrationTests.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/spring-data-jpa/src/test/java/org/springframework/data/jpa/repository/query/QueryWithNullLikeHibernateIntegrationTests.java b/spring-data-jpa/src/test/java/org/springframework/data/jpa/repository/query/QueryWithNullLikeHibernateIntegrationTests.java index d60dd48b0..fdc559b1f 100644 --- a/spring-data-jpa/src/test/java/org/springframework/data/jpa/repository/query/QueryWithNullLikeHibernateIntegrationTests.java +++ b/spring-data-jpa/src/test/java/org/springframework/data/jpa/repository/query/QueryWithNullLikeHibernateIntegrationTests.java @@ -31,6 +31,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan.Filter; import org.springframework.context.annotation.FilterType; +import org.springframework.core.convert.ConversionFailedException; import org.springframework.data.jpa.domain.sample.EmployeeWithName; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; @@ -263,12 +264,30 @@ public class QueryWithNullLikeHibernateIntegrationTests { "Bilbo Baggins"); } + @Test // GH-1184 + void mismatchedReturnTypeShouldCauseException() { + assertThatExceptionOfType(ConversionFailedException.class) + .isThrownBy(() -> repository.customQueryWithMismatchedReturnType()); + } + + @Test // GH-1184 + void alignedReturnTypeShouldWork() { + assertThat(repository.customQueryWithAlignedReturnType()).containsExactly(new Object[][] { + { "Frodo Baggins", "Frodo Baggins with suffix" }, { "Bilbo Baggins", "Bilbo Baggins with suffix" } }); + } + @Transactional public interface EmployeeWithNullLikeRepository extends JpaRepository { @Query("select e from EmployeeWithName e where e.name like %:partialName%") List customQueryWithNullableParam(@Nullable @Param("partialName") String partialName); + @Query("select e.name, concat(e.name, ' with suffix') from EmployeeWithName e") + List customQueryWithMismatchedReturnType(); + + @Query("select e.name, concat(e.name, ' with suffix') from EmployeeWithName e") + List customQueryWithAlignedReturnType(); + @Query(value = "select * from EmployeeWithName as e where e.name like %:partialName%", nativeQuery = true) List customQueryWithNullableParamInNative(@Nullable @Param("partialName") String partialName);