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 330de1fabd
commit 53e2c70088
2 changed files with 28 additions and 0 deletions

View File

@@ -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!");

View File

@@ -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);
}
}
}