DATAJPA-135 - Improved extensibility of QueryDslRepositorySupport.
Moved @PersistenceContext annotation to the setter method to make it overridable and thus re-configurable. Introduced protected getEntityManager() method to allow subclasses having access to the EntityManager.
This commit is contained in:
@@ -40,7 +40,6 @@ import com.mysema.query.types.path.PathBuilderFactory;
|
||||
@Repository
|
||||
public abstract class QueryDslRepositorySupport {
|
||||
|
||||
@PersistenceContext
|
||||
private EntityManager entityManager;
|
||||
private PathBuilderFactory builderFactory = new PathBuilderFactory();
|
||||
|
||||
@@ -49,6 +48,7 @@ public abstract class QueryDslRepositorySupport {
|
||||
*
|
||||
* @param entityManager must not be {@literal null}
|
||||
*/
|
||||
@PersistenceContext
|
||||
public void setEntityManager(EntityManager entityManager) {
|
||||
|
||||
Assert.notNull(entityManager);
|
||||
@@ -60,10 +60,18 @@ public abstract class QueryDslRepositorySupport {
|
||||
*/
|
||||
@PostConstruct
|
||||
public void validate() {
|
||||
|
||||
Assert.notNull(entityManager, "EntityManager must not be null!");
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the {@link EntityManager}.
|
||||
*
|
||||
* @return the entityManager
|
||||
*/
|
||||
protected EntityManager getEntityManager() {
|
||||
return entityManager;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a fresh {@link JPQLQuery}.
|
||||
*
|
||||
|
||||
@@ -18,6 +18,9 @@ package org.springframework.data.jpa.repository.support;
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.PersistenceContext;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -37,8 +40,39 @@ public class QueryDslRepositorySupportIntegrationTests {
|
||||
@Autowired
|
||||
UserRepository repository;
|
||||
|
||||
@Autowired
|
||||
ReconfiguringUserRepositoryImpl reconfiguredRepo;
|
||||
|
||||
@PersistenceContext(unitName = "querydsl")
|
||||
EntityManager em;
|
||||
|
||||
@Test
|
||||
public void createsRepoCorrectly() {
|
||||
assertThat(repository, is(notNullValue()));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAJPA-135
|
||||
*/
|
||||
@Test
|
||||
public void createsReconfiguredRepoAccordingly() {
|
||||
|
||||
assertThat(reconfiguredRepo, is(notNullValue()));
|
||||
assertThat(reconfiguredRepo.getEntityManager().getEntityManagerFactory(), is(em.getEntityManagerFactory()));
|
||||
}
|
||||
|
||||
static class ReconfiguringUserRepositoryImpl extends QueryDslRepositorySupport {
|
||||
|
||||
@Override
|
||||
@PersistenceContext(unitName = "querydsl")
|
||||
public void setEntityManager(EntityManager entityManager) {
|
||||
super.setEntityManager(entityManager);
|
||||
}
|
||||
}
|
||||
|
||||
static class EntityManagerContainer {
|
||||
|
||||
@PersistenceContext(unitName = "querydsl")
|
||||
EntityManager em;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -127,6 +127,12 @@ public class QueryDslRepositorySupportTests {
|
||||
|
||||
private static final QUser user = QUser.user;
|
||||
|
||||
@Override
|
||||
@PersistenceContext(unitName = "default")
|
||||
public void setEntityManager(EntityManager entityManager) {
|
||||
super.setEntityManager(entityManager);
|
||||
}
|
||||
|
||||
public List<User> findUsersByLastname(String lastname) {
|
||||
|
||||
return from(user).where(user.lastname.eq(lastname)).list(user);
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
|
||||
<persistence-unit name="hades">
|
||||
<persistence-unit name="default">
|
||||
<class>org.springframework.data.jpa.domain.AbstractPersistable</class>
|
||||
<class>org.springframework.data.jpa.domain.sample.User</class>
|
||||
<class>org.springframework.data.jpa.domain.sample.SpecialUser</class>
|
||||
@@ -13,4 +13,8 @@
|
||||
<class>org.springframework.data.jpa.domain.sample.SampleEntityPK</class>
|
||||
<exclude-unlisted-classes>true</exclude-unlisted-classes>
|
||||
</persistence-unit>
|
||||
<persistence-unit name="querydsl">
|
||||
<class>org.springframework.data.jpa.domain.sample.User</class>
|
||||
<exclude-unlisted-classes>true</exclude-unlisted-classes>
|
||||
</persistence-unit>
|
||||
</persistence>
|
||||
|
||||
@@ -6,5 +6,13 @@
|
||||
<import resource="infrastructure.xml" />
|
||||
|
||||
<bean class="org.springframework.data.jpa.repository.support.QueryDslRepositorySupportTests.UserRepositoryImpl" />
|
||||
|
||||
<bean class="org.springframework.data.jpa.repository.support.QueryDslRepositorySupportIntegrationTests.ReconfiguringUserRepositoryImpl" />
|
||||
|
||||
<bean class="org.springframework.data.jpa.repository.support.QueryDslRepositorySupportIntegrationTests.EntityManagerContainer" />
|
||||
|
||||
<bean id="alternate" parent="entityManagerFactory">
|
||||
<property name="persistenceUnitName" value="querydsl" />
|
||||
</bean>
|
||||
|
||||
</beans>
|
||||
|
||||
Reference in New Issue
Block a user