DATAJPA-710 - Adapt to changes in Spring Data Commons.

Tweaked method signatures in JpaRepositoryFactory after some signature changes in Spring Data Commons. Use newly introduced getTragetRepositoryViaReflection(…) to obtain the repository instance via the super class.

Added repositoryBaseClass() attribute to @EnableJpaRepositories.

Related tickets: DATACMNS-542.
This commit is contained in:
Oliver Gierke
2015-02-24 21:09:24 +01:00
parent f46cc8ac0b
commit 4474ef7f04
4 changed files with 41 additions and 15 deletions

View File

@@ -28,6 +28,7 @@ import org.springframework.beans.factory.FactoryBean;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.Import;
import org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean;
import org.springframework.data.repository.config.DefaultRepositoryBaseClass;
import org.springframework.data.repository.query.QueryLookupStrategy;
import org.springframework.data.repository.query.QueryLookupStrategy.Key;
import org.springframework.transaction.PlatformTransactionManager;
@@ -109,7 +110,16 @@ public @interface EnableJpaRepositories {
*/
Class<?> repositoryFactoryBeanClass() default JpaRepositoryFactoryBean.class;
/**
* Configure the repository base class to be used to create repository proxies for this particular configuration.
*
* @return
* @since 1.9
*/
Class<?> repositoryBaseClass() default DefaultRepositoryBaseClass.class;
// JPA specific configuration
/**
* Configures the name of the {@link EntityManagerFactory} bean definition to be used to create repositories
* discovered through this annotation. Defaults to {@code entityManagerFactory}.

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2008-2014 the original author or authors.
* Copyright 2008-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -26,6 +26,7 @@ import org.springframework.data.jpa.provider.QueryExtractor;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy;
import org.springframework.data.querydsl.QueryDslPredicateExecutor;
import org.springframework.data.repository.core.RepositoryInformation;
import org.springframework.data.repository.core.RepositoryMetadata;
import org.springframework.data.repository.core.support.RepositoryFactorySupport;
import org.springframework.data.repository.query.EvaluationContextProvider;
@@ -65,9 +66,9 @@ public class JpaRepositoryFactory extends RepositoryFactorySupport {
* @see org.springframework.data.repository.core.support.RepositoryFactorySupport#getTargetRepository(org.springframework.data.repository.core.RepositoryMetadata)
*/
@Override
protected Object getTargetRepository(RepositoryMetadata metadata) {
protected Object getTargetRepository(RepositoryInformation information) {
SimpleJpaRepository<?, ?> repository = getTargetRepository(metadata, entityManager);
SimpleJpaRepository<?, ?> repository = getTargetRepository(information, entityManager);
repository.setRepositoryMethodMetadata(lockModePostProcessor.getLockMetadataProvider());
return repository;
@@ -82,17 +83,12 @@ public class JpaRepositoryFactory extends RepositoryFactorySupport {
* @see #getTargetRepository(RepositoryMetadata)
* @return
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
protected <T, ID extends Serializable> SimpleJpaRepository<?, ?> getTargetRepository(RepositoryMetadata metadata,
EntityManager entityManager) {
protected <T, ID extends Serializable> SimpleJpaRepository<?, ?> getTargetRepository(
RepositoryInformation information, EntityManager entityManager) {
Class<?> repositoryInterface = metadata.getRepositoryInterface();
JpaEntityInformation<?, Serializable> entityInformation = getEntityInformation(metadata.getDomainType());
JpaEntityInformation<?, Serializable> entityInformation = getEntityInformation(information.getDomainType());
SimpleJpaRepository<?, ?> repo = isQueryDslExecutor(repositoryInterface) ? new QueryDslJpaRepository(
entityInformation, entityManager) : new SimpleJpaRepository(entityInformation, entityManager);
return repo;
return getTargetRepositoryViaReflection(information, entityInformation, entityManager);
}
/*

View File

@@ -24,6 +24,7 @@ import javax.persistence.EntityManager;
import org.springframework.data.jpa.repository.support.JpaEntityInformation;
import org.springframework.data.jpa.repository.support.JpaRepositoryFactory;
import org.springframework.data.jpa.repository.support.SimpleJpaRepository;
import org.springframework.data.repository.core.RepositoryInformation;
import org.springframework.data.repository.core.RepositoryMetadata;
/**
@@ -47,10 +48,10 @@ public class CustomGenericJpaRepositoryFactory extends JpaRepositoryFactory {
*/
@Override
@SuppressWarnings("unchecked")
protected SimpleJpaRepository<?, ?> getTargetRepository(RepositoryMetadata metadata, EntityManager em) {
protected SimpleJpaRepository<?, ?> getTargetRepository(RepositoryInformation information, EntityManager em) {
JpaEntityInformation<Object, Serializable> entityMetadata = mock(JpaEntityInformation.class);
when(entityMetadata.getJavaType()).thenReturn((Class<Object>) metadata.getDomainType());
when(entityMetadata.getJavaType()).thenReturn((Class<Object>) information.getDomainType());
return new CustomGenericJpaRepository<Object, Serializable>(entityMetadata, em);
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2008-2014 the original author or authors.
* Copyright 2008-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -152,6 +152,18 @@ public class JpaRepositoryFactoryUnitTests {
}
}
/**
* @see DATAJPA-710, DATACMNS-542
*/
@Test
public void usesConfiguredRepositoryBaseClass() {
factory.setRepositoryBaseClass(CustomJpaRepository.class);
SampleRepository repository = factory.getRepository(SampleRepository.class);
assertEquals(CustomJpaRepository.class, ((Advised) repository).getTargetClass());
}
private interface SimpleSampleRepository extends JpaRepository<User, Integer> {
@Transactional
@@ -195,4 +207,11 @@ public class JpaRepositoryFactoryUnitTests {
private interface QueryDslSampleRepository extends SimpleSampleRepository, QueryDslPredicateExecutor<User> {
}
static class CustomJpaRepository<T, ID extends Serializable> extends SimpleJpaRepository<T, ID> {
public CustomJpaRepository(JpaEntityInformation<T, ?> entityInformation, EntityManager entityManager) {
super(entityInformation, entityManager);
}
};
}