From 4474ef7f049f97e93e12976a3c708e281f0c7470 Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Tue, 24 Feb 2015 21:09:24 +0100 Subject: [PATCH] DATAJPA-710 - Adapt to changes in Spring Data Commons. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- .../config/EnableJpaRepositories.java | 10 +++++++++ .../support/JpaRepositoryFactory.java | 20 +++++++----------- .../CustomGenericJpaRepositoryFactory.java | 5 +++-- .../JpaRepositoryFactoryUnitTests.java | 21 ++++++++++++++++++- 4 files changed, 41 insertions(+), 15 deletions(-) diff --git a/src/main/java/org/springframework/data/jpa/repository/config/EnableJpaRepositories.java b/src/main/java/org/springframework/data/jpa/repository/config/EnableJpaRepositories.java index 96826f684..a93925cc9 100644 --- a/src/main/java/org/springframework/data/jpa/repository/config/EnableJpaRepositories.java +++ b/src/main/java/org/springframework/data/jpa/repository/config/EnableJpaRepositories.java @@ -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}. diff --git a/src/main/java/org/springframework/data/jpa/repository/support/JpaRepositoryFactory.java b/src/main/java/org/springframework/data/jpa/repository/support/JpaRepositoryFactory.java index 96cb0b531..15209a7a4 100644 --- a/src/main/java/org/springframework/data/jpa/repository/support/JpaRepositoryFactory.java +++ b/src/main/java/org/springframework/data/jpa/repository/support/JpaRepositoryFactory.java @@ -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 SimpleJpaRepository getTargetRepository(RepositoryMetadata metadata, - EntityManager entityManager) { + protected SimpleJpaRepository getTargetRepository( + RepositoryInformation information, EntityManager entityManager) { - Class repositoryInterface = metadata.getRepositoryInterface(); - JpaEntityInformation entityInformation = getEntityInformation(metadata.getDomainType()); + JpaEntityInformation entityInformation = getEntityInformation(information.getDomainType()); - SimpleJpaRepository repo = isQueryDslExecutor(repositoryInterface) ? new QueryDslJpaRepository( - entityInformation, entityManager) : new SimpleJpaRepository(entityInformation, entityManager); - - return repo; + return getTargetRepositoryViaReflection(information, entityInformation, entityManager); } /* diff --git a/src/test/java/org/springframework/data/jpa/repository/custom/CustomGenericJpaRepositoryFactory.java b/src/test/java/org/springframework/data/jpa/repository/custom/CustomGenericJpaRepositoryFactory.java index 4cec44383..4bcbc3700 100644 --- a/src/test/java/org/springframework/data/jpa/repository/custom/CustomGenericJpaRepositoryFactory.java +++ b/src/test/java/org/springframework/data/jpa/repository/custom/CustomGenericJpaRepositoryFactory.java @@ -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 entityMetadata = mock(JpaEntityInformation.class); - when(entityMetadata.getJavaType()).thenReturn((Class) metadata.getDomainType()); + when(entityMetadata.getJavaType()).thenReturn((Class) information.getDomainType()); return new CustomGenericJpaRepository(entityMetadata, em); } diff --git a/src/test/java/org/springframework/data/jpa/repository/support/JpaRepositoryFactoryUnitTests.java b/src/test/java/org/springframework/data/jpa/repository/support/JpaRepositoryFactoryUnitTests.java index c470443d7..444658cdb 100644 --- a/src/test/java/org/springframework/data/jpa/repository/support/JpaRepositoryFactoryUnitTests.java +++ b/src/test/java/org/springframework/data/jpa/repository/support/JpaRepositoryFactoryUnitTests.java @@ -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 { @Transactional @@ -195,4 +207,11 @@ public class JpaRepositoryFactoryUnitTests { private interface QueryDslSampleRepository extends SimpleSampleRepository, QueryDslPredicateExecutor { } + + static class CustomJpaRepository extends SimpleJpaRepository { + + public CustomJpaRepository(JpaEntityInformation entityInformation, EntityManager entityManager) { + super(entityInformation, entityManager); + } + }; }