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:
Oliver Gierke
2011-12-09 13:02:20 +01:00
parent bb124e1e58
commit fe36a77f38
5 changed files with 63 additions and 3 deletions

View File

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

View File

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