-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix type information when using dinamic projection and generic reposi…
…tory
- Loading branch information
Showing
4 changed files
with
77 additions
and
37 deletions.
There are no files selected for viewing
44 changes: 21 additions & 23 deletions
44
...m/cosium/spring/data/jpa/entity/graph/repository/query/EntityGraphAwareJpaParameters.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} | ||
} |
28 changes: 15 additions & 13 deletions
28
.../cosium/spring/data/jpa/entity/graph/repository/query/EntityGraphAwareJpaQueryMethod.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
...a/com/cosium/spring/data/jpa/entity/graph/repository/DynamicProjectionRepositoryTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters