From 53e2c7008895652cf418109e3550d23ee91ad8a7 Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Mon, 16 Oct 2017 12:19:51 +0200 Subject: [PATCH] DATAJPA-1205 - Reinstantiate transparent EntityManager injection in QueryDslRepositorySupport. We now re-instantiated entity manager injection in QueryDslRepositorySupport using @Autowired. Previously, we removed dependency injection through @PersistenceContext to avoid bootstrap errors caused by class scanning in application containers signaled by deployment annotation use. Related ticket: DATAJPA-1175. Original pull request: #227. --- .../support/QueryDslRepositorySupport.java | 2 ++ ...yDslRepositorySupportIntegrationTests.java | 26 +++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/src/main/java/org/springframework/data/jpa/repository/support/QueryDslRepositorySupport.java b/src/main/java/org/springframework/data/jpa/repository/support/QueryDslRepositorySupport.java index e8836066b..3a352f353 100644 --- a/src/main/java/org/springframework/data/jpa/repository/support/QueryDslRepositorySupport.java +++ b/src/main/java/org/springframework/data/jpa/repository/support/QueryDslRepositorySupport.java @@ -18,6 +18,7 @@ package org.springframework.data.jpa.repository.support; import javax.annotation.PostConstruct; import javax.persistence.EntityManager; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Repository; import org.springframework.util.Assert; @@ -60,6 +61,7 @@ public abstract class QueryDslRepositorySupport { * * @param entityManager must not be {@literal null}. */ + @Autowired public void setEntityManager(EntityManager entityManager) { Assert.notNull(entityManager, "EntityManager must not be null!"); diff --git a/src/test/java/org/springframework/data/jpa/repository/support/QueryDslRepositorySupportIntegrationTests.java b/src/test/java/org/springframework/data/jpa/repository/support/QueryDslRepositorySupportIntegrationTests.java index 38e2ef44c..b70a589d8 100644 --- a/src/test/java/org/springframework/data/jpa/repository/support/QueryDslRepositorySupportIntegrationTests.java +++ b/src/test/java/org/springframework/data/jpa/repository/support/QueryDslRepositorySupportIntegrationTests.java @@ -41,6 +41,7 @@ import org.springframework.transaction.annotation.Transactional; * * @author Oliver Gierke * @author Thomas Darimont + * @author Mark Paluch */ @Transactional @ContextConfiguration @@ -61,11 +62,21 @@ public class QueryDslRepositorySupportIntegrationTests { }; } + @Bean + EntityManagerBeanDefinitionRegistrarPostProcessor entityManagerBeanDefinitionRegistrarPostProcessor() { + return new EntityManagerBeanDefinitionRegistrarPostProcessor(); + } + @Bean public ReconfiguringUserRepositoryImpl reconfiguringUserRepositoryImpl() { return new ReconfiguringUserRepositoryImpl(); } + @Bean + public CustomRepoUsingQueryDsl customRepo() { + return new CustomRepoUsingQueryDsl(); + } + @Bean public EntityManagerContainer entityManagerContainer() { return new EntityManagerContainer(); @@ -82,6 +93,7 @@ public class QueryDslRepositorySupportIntegrationTests { } @Autowired UserRepository repository; + @Autowired CustomRepoUsingQueryDsl querydslCustom; @Autowired ReconfiguringUserRepositoryImpl reconfiguredRepo; @PersistenceContext(unitName = "querydsl") EntityManager em; @@ -98,6 +110,13 @@ public class QueryDslRepositorySupportIntegrationTests { assertThat(reconfiguredRepo.getEntityManager().getEntityManagerFactory(), is(em.getEntityManagerFactory())); } + @Test // DATAJPA-1205 + public void createsRepositoryWithCustomImplementationUsingQueryDsl() { + + assertThat(querydslCustom, is(notNullValue())); + assertThat(querydslCustom.getEntityManager().getEntityManagerFactory(), is(em.getEntityManagerFactory())); + } + static class ReconfiguringUserRepositoryImpl extends QueryDslRepositorySupport { public ReconfiguringUserRepositoryImpl() { @@ -115,4 +134,11 @@ public class QueryDslRepositorySupportIntegrationTests { @PersistenceContext(unitName = "querydsl") EntityManager em; } + + static class CustomRepoUsingQueryDsl extends QueryDslRepositorySupport { + + public CustomRepoUsingQueryDsl() { + super(User.class); + } + } }