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.
This commit is contained in:
Mark Paluch
2017-10-16 12:19:51 +02:00
committed by Oliver Gierke
parent 883a8b424a
commit f7e83d54d0
2 changed files with 28 additions and 0 deletions

View File

@@ -19,6 +19,7 @@ import javax.annotation.Nullable;
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;
@@ -61,6 +62,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!");

View File

@@ -40,6 +40,7 @@ import org.springframework.transaction.annotation.Transactional;
*
* @author Oliver Gierke
* @author Thomas Darimont
* @author Mark Paluch
*/
@Transactional
@ContextConfiguration
@@ -60,11 +61,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();
@@ -81,6 +92,7 @@ public class QuerydslRepositorySupportIntegrationTests {
}
@Autowired org.springframework.data.jpa.repository.support.QuerydslRepositorySupportTests.UserRepository repository;
@Autowired CustomRepoUsingQueryDsl querydslCustom;
@Autowired ReconfiguringUserRepositoryImpl reconfiguredRepo;
@PersistenceContext(unitName = "querydsl") EntityManager em;
@@ -97,6 +109,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() {
@@ -114,4 +133,11 @@ public class QuerydslRepositorySupportIntegrationTests {
@PersistenceContext(unitName = "querydsl") EntityManager em;
}
static class CustomRepoUsingQueryDsl extends QuerydslRepositorySupport {
public CustomRepoUsingQueryDsl() {
super(User.class);
}
}
}