Skip to content

Commit

Permalink
Fix type information when using dynamic projection and generic reposi…
Browse files Browse the repository at this point in the history
…tory (#149)

Co-authored-by: Réda Housni Alaoui <[email protected]>
  • Loading branch information
leassis and reda-alaoui authored Dec 27, 2023
1 parent 825f5fb commit 4bcc022
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 15 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,8 @@ You can play with https://github.com/Cosium/spring-data-jpa-entity-graph-sample

| [Spring Data JPA](https://github.com/spring-projects/spring-data-jpa) version | [Spring Data JPA EntityGraph](https://github.com/Cosium/spring-data-jpa-entity-graph) version |
|-------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 3.2.x | [![Maven Central 3.2.x](https://img.shields.io/maven-central/v/com.cosium.spring.data/spring-data-jpa-entity-graph/3.2.svg)](https://search.maven.org/#search%7Cgav%7C1%7Cg%3A%22com.cosium.spring.data%22%20AND%20a%3A%22spring-data-jpa-entity-graph%22) |
| 3.2.1+ | [![Maven Central 3.2.2+](https://img.shields.io/maven-central/v/com.cosium.spring.data/spring-data-jpa-entity-graph/3.2.2.svg)](https://search.maven.org/#search%7Cgav%7C1%7Cg%3A%22com.cosium.spring.data%22%20AND%20a%3A%22spring-data-jpa-entity-graph%22) |
| 3.2.0 | [![Maven Central 3.2.1](https://img.shields.io/maven-central/v/com.cosium.spring.data/spring-data-jpa-entity-graph/3.2.1.svg)](https://search.maven.org/#search%7Cgav%7C1%7Cg%3A%22com.cosium.spring.data%22%20AND%20a%3A%22spring-data-jpa-entity-graph%22) |
| 3.1.x | [![Maven Central 3.1.x](https://img.shields.io/maven-central/v/com.cosium.spring.data/spring-data-jpa-entity-graph/3.1.svg)](https://search.maven.org/#search%7Cgav%7C1%7Cg%3A%22com.cosium.spring.data%22%20AND%20a%3A%22spring-data-jpa-entity-graph%22) |
| 3.0.x | [![Maven Central 3.0.x](https://img.shields.io/maven-central/v/com.cosium.spring.data/spring-data-jpa-entity-graph/3.0.svg)](https://search.maven.org/#search%7Cgav%7C1%7Cg%3A%22com.cosium.spring.data%22%20AND%20a%3A%22spring-data-jpa-entity-graph%22) |
| 2.7.x | [![Maven Central 2.7.x](https://img.shields.io/maven-central/v/com.cosium.spring.data/spring-data-jpa-entity-graph/2.7.svg)](https://search.maven.org/#search%7Cgav%7C1%7Cg%3A%22com.cosium.spring.data%22%20AND%20a%3A%22spring-data-jpa-entity-graph%22) |
Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
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 final boolean entityGraph;

protected EntityGraphAwareJpaParameter(MethodParameter parameter) {
super(parameter);
private EntityGraphAwareJpaParameter(MethodParameter parameter, TypeInformation<?> domainType) {
super(parameter, domainType);
this.entityGraph = EntityGraph.class.isAssignableFrom(parameter.getParameterType());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

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;

/**
* @author Réda Housni Alaoui
Expand All @@ -21,7 +22,7 @@ protected EntityGraphAwareJpaQueryMethod(
}

@Override
protected JpaParameters createParameters(Method method) {
return new EntityGraphAwareJpaParameters(method);
protected Parameters<?, ?> createParameters(ParametersSource parametersSource) {
return new EntityGraphAwareJpaParameters(parametersSource);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.cosium.spring.data.jpa.entity.graph.repository;

import static org.assertj.core.api.Assertions.assertThat;

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 java.io.Serializable;
import java.util.Optional;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.data.repository.NoRepositoryBean;
import org.springframework.data.repository.Repository;

@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<T, I extends Serializable> extends Repository<T, I> {
<X> Optional<X> findById(I id, Class<X> clazz);
}

@org.springframework.stereotype.Repository
public interface BrandRepository extends EntityGraphBaseRepository<Brand, Long> {}
}
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<properties>
<maven.compiler.release>17</maven.compiler.release>

<spring.data.jpa>3.2.0</spring.data.jpa>
<spring.data.jpa>3.2.1</spring.data.jpa>
<querydsl>5.0.0</querydsl>

<dbunit>2.7.3</dbunit>
Expand Down

0 comments on commit 4bcc022

Please sign in to comment.