Skip to content

Commit

Permalink
fix type information when using dinamic projection and generic reposi…
Browse files Browse the repository at this point in the history
…tory
  • Loading branch information
leassis committed Dec 25, 2023
1 parent 15aa62d commit 72fae93
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 37 deletions.
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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<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 72fae93

Please sign in to comment.