Skip to content

Commit f780374

Browse files
committed
DATAJPA-1230 - Polishing.
Introduced JpaRepositoryImplementation interface to combine JpaRepository, JpaSpecificationExecutor and a callback to set CrudMethodMetadata on the implementation instance. Refactored JpaRepositoryFactory to only rely on that interface and avoid references to SimpleJpaRepository.
1 parent 31ddaa9 commit f780374

File tree

3 files changed

+24
-20
lines changed

3 files changed

+24
-20
lines changed

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

+11-11
Original file line numberDiff line numberDiff line change
@@ -86,30 +86,30 @@ public void setBeanClassLoader(ClassLoader classLoader) {
8686
* @see org.springframework.data.repository.core.support.RepositoryFactorySupport#getTargetRepository(org.springframework.data.repository.core.RepositoryMetadata)
8787
*/
8888
@Override
89-
protected Object getTargetRepository(RepositoryInformation information) {
89+
protected final JpaRepositoryImplementation<?, ?> getTargetRepository(RepositoryInformation information) {
9090

91-
Object repository = getTargetRepository(information, entityManager);
92-
if (repository instanceof RepositoryMethodMetadataAware) {
93-
((RepositoryMethodMetadataAware) repository)
94-
.setRepositoryMethodMetadata(crudMethodMetadataPostProcessor.getCrudMethodMetadata());
95-
}
91+
JpaRepositoryImplementation<?, ?> repository = getTargetRepository(information, entityManager);
92+
repository.setRepositoryMethodMetadata(crudMethodMetadataPostProcessor.getCrudMethodMetadata());
9693

9794
return repository;
9895
}
9996

10097
/**
10198
* Callback to create a {@link JpaRepository} instance with the given {@link EntityManager}
10299
*
103-
* @param <T>
104-
* @param <ID>
105-
* @param entityManager
100+
* @param information will never be {@literal null}.
101+
* @param entityManager will never be {@literal null}.
106102
* @return
107103
*/
108-
protected Object getTargetRepository(RepositoryInformation information, EntityManager entityManager) {
104+
protected JpaRepositoryImplementation<?, ?> getTargetRepository(RepositoryInformation information,
105+
EntityManager entityManager) {
109106

110107
JpaEntityInformation<?, Serializable> entityInformation = getEntityInformation(information.getDomainType());
108+
Object repository = getTargetRepositoryViaReflection(information, entityInformation, entityManager);
109+
110+
Assert.isInstanceOf(JpaRepositoryImplementation.class, repository);
111111

112-
return getTargetRepositoryViaReflection(information, entityInformation, entityManager);
112+
return (JpaRepositoryImplementation<?, ?>) repository;
113113
}
114114

115115
/*

src/main/java/org/springframework/data/jpa/repository/support/RepositoryMethodMetadataAware.java renamed to src/main/java/org/springframework/data/jpa/repository/support/JpaRepositoryImplementation.java

+12-5
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,23 @@
1515
*/
1616
package org.springframework.data.jpa.repository.support;
1717

18-
import org.springframework.data.jpa.repository.support.CrudMethodMetadata;
18+
import org.springframework.data.jpa.repository.JpaRepository;
19+
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
20+
import org.springframework.data.repository.NoRepositoryBean;
1921

2022
/**
21-
* implemented by {@link org.springframework.data.jpa.repository.JpaRepository} implementations requiring
22-
* {@link CrudMethodMetadata}
23+
* SPI interface to be implemented by {@link JpaRepository} implementations.
2324
*
25+
* @author Oliver Gierke
2426
* @author Stefan Fussenegger
2527
*/
26-
public interface RepositoryMethodMetadataAware {
28+
@NoRepositoryBean
29+
public interface JpaRepositoryImplementation<T, ID> extends JpaRepository<T, ID>, JpaSpecificationExecutor<T> {
2730

31+
/**
32+
* Configures the {@link CrudMethodMetadata} to be used with the repository.
33+
*
34+
* @param crudMethodMetadata must not be {@literal null}.
35+
*/
2836
void setRepositoryMethodMetadata(CrudMethodMetadata crudMethodMetadata);
29-
3037
}

src/main/java/org/springframework/data/jpa/repository/support/SimpleJpaRepository.java

+1-4
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,6 @@
4848
import org.springframework.data.jpa.domain.Specification;
4949
import org.springframework.data.jpa.provider.PersistenceProvider;
5050
import org.springframework.data.jpa.repository.EntityGraph;
51-
import org.springframework.data.jpa.repository.JpaRepository;
52-
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
5351
import org.springframework.data.jpa.repository.query.QueryUtils;
5452
import org.springframework.data.jpa.repository.support.QueryHints.NoHints;
5553
import org.springframework.data.repository.support.PageableExecutionUtils;
@@ -73,8 +71,7 @@
7371
*/
7472
@Repository
7573
@Transactional(readOnly = true)
76-
public class SimpleJpaRepository<T, ID>
77-
implements JpaRepository<T, ID>, JpaSpecificationExecutor<T>, RepositoryMethodMetadataAware {
74+
public class SimpleJpaRepository<T, ID> implements JpaRepositoryImplementation<T, ID> {
7875

7976
private static final String ID_MUST_NOT_BE_NULL = "The given id must not be null!";
8077

0 commit comments

Comments
 (0)