Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix type information when using dynamic projection and generic repository #149

Merged
merged 6 commits into from
Dec 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading