From 72fae9313a554075486e6a7a0fda6bcf62dc54a5 Mon Sep 17 00:00:00 2001 From: lassis Date: Mon, 25 Dec 2023 17:13:52 +0100 Subject: [PATCH] fix type information when using dinamic projection and generic repository --- .../query/EntityGraphAwareJpaParameters.java | 44 +++++++++---------- .../query/EntityGraphAwareJpaQueryMethod.java | 28 ++++++------ .../DynamicProjectionRepositoryTest.java | 40 +++++++++++++++++ pom.xml | 2 +- 4 files changed, 77 insertions(+), 37 deletions(-) create mode 100644 core/src/test/java/com/cosium/spring/data/jpa/entity/graph/repository/DynamicProjectionRepositoryTest.java diff --git a/core/src/main/java/com/cosium/spring/data/jpa/entity/graph/repository/query/EntityGraphAwareJpaParameters.java b/core/src/main/java/com/cosium/spring/data/jpa/entity/graph/repository/query/EntityGraphAwareJpaParameters.java index 597a81a..e9a1810 100644 --- a/core/src/main/java/com/cosium/spring/data/jpa/entity/graph/repository/query/EntityGraphAwareJpaParameters.java +++ b/core/src/main/java/com/cosium/spring/data/jpa/entity/graph/repository/query/EntityGraphAwareJpaParameters.java @@ -1,41 +1,39 @@ package com.cosium.spring.data.jpa.entity.graph.repository.query; import com.cosium.spring.data.jpa.entity.graph.domain2.EntityGraph; -import java.lang.reflect.Method; import org.springframework.core.MethodParameter; import org.springframework.data.jpa.repository.query.JpaParameters; +import org.springframework.data.repository.query.ParametersSource; +import org.springframework.data.util.TypeInformation; /** * @author Réda Housni Alaoui */ class EntityGraphAwareJpaParameters extends JpaParameters { - public EntityGraphAwareJpaParameters(Method method) { - super(method); - } - - @Override - protected JpaParameter createParameter(MethodParameter parameter) { - return new EntityGraphAwareJpaParameter(parameter); - } + public EntityGraphAwareJpaParameters(ParametersSource parametersSource) { + super(parametersSource, methodParameter -> + new EntityGraphAwareJpaParameter(methodParameter, parametersSource.getDomainTypeInformation()) + ); + } - private static class EntityGraphAwareJpaParameter extends JpaParameters.JpaParameter { + private static class EntityGraphAwareJpaParameter extends JpaParameters.JpaParameter { - private final boolean entityGraph; + private final boolean entityGraph; - protected EntityGraphAwareJpaParameter(MethodParameter parameter) { - super(parameter); - this.entityGraph = EntityGraph.class.isAssignableFrom(parameter.getParameterType()); - } + private EntityGraphAwareJpaParameter(MethodParameter parameter, TypeInformation domainType) { + super(parameter, domainType); + this.entityGraph = EntityGraph.class.isAssignableFrom(parameter.getParameterType()); + } - @Override - public boolean isBindable() { - return !entityGraph && super.isBindable(); - } + @Override + public boolean isBindable() { + return !entityGraph && super.isBindable(); + } - @Override - public boolean isSpecialParameter() { - return entityGraph || super.isSpecialParameter(); + @Override + public boolean isSpecialParameter() { + return entityGraph || super.isSpecialParameter(); + } } - } } diff --git a/core/src/main/java/com/cosium/spring/data/jpa/entity/graph/repository/query/EntityGraphAwareJpaQueryMethod.java b/core/src/main/java/com/cosium/spring/data/jpa/entity/graph/repository/query/EntityGraphAwareJpaQueryMethod.java index f76e0c3..60af8a6 100644 --- a/core/src/main/java/com/cosium/spring/data/jpa/entity/graph/repository/query/EntityGraphAwareJpaQueryMethod.java +++ b/core/src/main/java/com/cosium/spring/data/jpa/entity/graph/repository/query/EntityGraphAwareJpaQueryMethod.java @@ -1,27 +1,29 @@ package com.cosium.spring.data.jpa.entity.graph.repository.query; -import java.lang.reflect.Method; import org.springframework.data.jpa.provider.QueryExtractor; -import org.springframework.data.jpa.repository.query.JpaParameters; import org.springframework.data.jpa.repository.query.JpaQueryMethod; import org.springframework.data.projection.ProjectionFactory; import org.springframework.data.repository.core.RepositoryMetadata; +import org.springframework.data.repository.query.Parameters; +import org.springframework.data.repository.query.ParametersSource; + +import java.lang.reflect.Method; /** * @author Réda Housni Alaoui */ class EntityGraphAwareJpaQueryMethod extends JpaQueryMethod { - protected EntityGraphAwareJpaQueryMethod( - Method method, - RepositoryMetadata metadata, - ProjectionFactory factory, - QueryExtractor extractor) { - super(method, metadata, factory, extractor); - } + protected EntityGraphAwareJpaQueryMethod( + Method method, + RepositoryMetadata metadata, + ProjectionFactory factory, + QueryExtractor extractor) { + super(method, metadata, factory, extractor); + } - @Override - protected JpaParameters createParameters(Method method) { - return new EntityGraphAwareJpaParameters(method); - } + @Override + protected Parameters createParameters(ParametersSource parametersSource) { + return new EntityGraphAwareJpaParameters(parametersSource); + } } diff --git a/core/src/test/java/com/cosium/spring/data/jpa/entity/graph/repository/DynamicProjectionRepositoryTest.java b/core/src/test/java/com/cosium/spring/data/jpa/entity/graph/repository/DynamicProjectionRepositoryTest.java new file mode 100644 index 0000000..6160e5f --- /dev/null +++ b/core/src/test/java/com/cosium/spring/data/jpa/entity/graph/repository/DynamicProjectionRepositoryTest.java @@ -0,0 +1,40 @@ +package com.cosium.spring.data.jpa.entity.graph.repository; + +import com.cosium.spring.data.jpa.entity.graph.BaseTest; +import com.cosium.spring.data.jpa.entity.graph.sample.Brand; +import com.github.springtestdbunit.annotation.DatabaseSetup; +import jakarta.inject.Inject; +import jakarta.transaction.Transactional; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.springframework.data.repository.NoRepositoryBean; +import org.springframework.data.repository.Repository; + +import java.io.Serializable; +import java.util.Optional; + +import static org.assertj.core.api.Assertions.assertThat; + +@DatabaseSetup(BaseTest.DATASET) +class DynamicProjectionRepositoryTest extends BaseTest { + + @Inject + private BrandRepository repository; + + @Test + @Transactional + @DisplayName("dynamic projections should work when you have a super class with generics") + void test1() { + var result = repository.findById(1L, Brand.class); + assertThat(result).map(Brand::getId).isPresent(); + + } + + @NoRepositoryBean + public interface EntityGraphBaseRepository extends Repository { + Optional findById(I id, Class clazz); + } + + @org.springframework.stereotype.Repository + public interface BrandRepository extends EntityGraphBaseRepository {} +} diff --git a/pom.xml b/pom.xml index 17d1cc9..6947141 100644 --- a/pom.xml +++ b/pom.xml @@ -19,7 +19,7 @@ 17 - 3.2.0 + 3.2.1 5.0.0 2.7.3