Skip to content

Commit 97fae57

Browse files
committed
Introduce template method for easier customization of fragments.
We introduced getRepositoryFragments(RepositoryMetadata,EntityManager,EntityPathResolver,CrudMethodMetadata) to easier get hold of typical arguments required for customization of repository base fragments that aren't bound to a specific entity type such as QuerydslJpaPredicateExecutor. Closes #2202.
1 parent dbf54e1 commit 97fae57

File tree

1 file changed

+27
-13
lines changed

1 file changed

+27
-13
lines changed

Diff for: src/main/java/org/springframework/data/jpa/repository/support/JpaRepositoryFactory.java

+27-13
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616
package org.springframework.data.jpa.repository.support;
1717

18+
import static org.springframework.data.querydsl.QuerydslUtils.*;
19+
1820
import java.io.Serializable;
1921
import java.lang.reflect.Method;
2022
import java.util.Optional;
@@ -23,6 +25,7 @@
2325
import javax.persistence.EntityManager;
2426
import javax.persistence.Tuple;
2527

28+
import com.querydsl.core.types.EntityPath;
2629
import org.slf4j.Logger;
2730

2831
import org.springframework.beans.factory.BeanFactory;
@@ -45,9 +48,8 @@
4548
import org.springframework.data.repository.core.RepositoryInformation;
4649
import org.springframework.data.repository.core.RepositoryMetadata;
4750
import org.springframework.data.repository.core.support.QueryCreationListener;
48-
import org.springframework.data.repository.core.support.RepositoryComposition;
51+
import org.springframework.data.repository.core.support.RepositoryComposition.RepositoryFragments;
4952
import org.springframework.data.repository.core.support.RepositoryFactorySupport;
50-
import org.springframework.data.repository.core.support.RepositoryFragment;
5153
import org.springframework.data.repository.core.support.SurroundingTransactionDetectorMethodInterceptor;
5254
import org.springframework.data.repository.query.QueryLookupStrategy;
5355
import org.springframework.data.repository.query.QueryLookupStrategy.Key;
@@ -57,8 +59,6 @@
5759
import org.springframework.util.Assert;
5860
import org.springframework.util.ReflectionUtils;
5961

60-
import static org.springframework.data.querydsl.QuerydslUtils.*;
61-
6262
/**
6363
* JPA specific generic repository factory.
6464
*
@@ -234,9 +234,27 @@ public <T, ID> JpaEntityInformation<T, ID> getEntityInformation(Class<T> domainC
234234
* @see org.springframework.data.repository.core.support.RepositoryFactorySupport#getRepositoryFragments(org.springframework.data.repository.core.RepositoryMetadata)
235235
*/
236236
@Override
237-
protected RepositoryComposition.RepositoryFragments getRepositoryFragments(RepositoryMetadata metadata) {
237+
protected RepositoryFragments getRepositoryFragments(RepositoryMetadata metadata) {
238+
239+
return getRepositoryFragments(metadata, entityManager, entityPathResolver,
240+
crudMethodMetadataPostProcessor.getCrudMethodMetadata());
241+
}
238242

239-
RepositoryComposition.RepositoryFragments fragments = RepositoryComposition.RepositoryFragments.empty();
243+
/**
244+
* Creates {@link RepositoryFragments} based on {@link RepositoryMetadata} to add JPA-specific extensions. Typically
245+
* adds a {@link QuerydslJpaPredicateExecutor} if the repository interface uses Querydsl.
246+
* <p>
247+
* Can be overridden by subclasses to customize {@link RepositoryFragments}.
248+
*
249+
* @param metadata repository metadata.
250+
* @param entityManager the entity manager.
251+
* @param resolver resolver to translate an plain domain class into a {@link EntityPath}.
252+
* @param crudMethodMetadata metadata about the invoked CRUD methods.
253+
* @return
254+
* @since 2.5.1
255+
*/
256+
protected RepositoryFragments getRepositoryFragments(RepositoryMetadata metadata, EntityManager entityManager,
257+
EntityPathResolver resolver, CrudMethodMetadata crudMethodMetadata) {
240258

241259
boolean isQueryDslRepository = QUERY_DSL_PRESENT
242260
&& QuerydslPredicateExecutor.class.isAssignableFrom(metadata.getRepositoryInterface());
@@ -248,15 +266,11 @@ protected RepositoryComposition.RepositoryFragments getRepositoryFragments(Repos
248266
"Cannot combine Querydsl and reactive repository support in a single interface");
249267
}
250268

251-
JpaEntityInformation<?, Serializable> entityInformation = getEntityInformation(metadata.getDomainType());
252-
253-
Object querydslFragment = getTargetRepositoryViaReflection(QuerydslJpaPredicateExecutor.class, entityInformation,
254-
entityManager, entityPathResolver, crudMethodMetadataPostProcessor.getCrudMethodMetadata());
255-
256-
fragments = fragments.append(RepositoryFragment.implemented(querydslFragment));
269+
return RepositoryFragments.just(new QuerydslJpaPredicateExecutor<>(getEntityInformation(metadata.getDomainType()),
270+
entityManager, resolver, crudMethodMetadata));
257271
}
258272

259-
return fragments;
273+
return RepositoryFragments.empty();
260274
}
261275

262276
private static boolean hasMethodReturningStream(Class<?> repositoryClass) {

0 commit comments

Comments
 (0)