DATAJPA-612 - Allow @EntityGraph on a method inherited from JpaRepository.
We now respect the @EntityGraph annotations on redeclared CRUD methods. CrudMethodMetadata now looks for @EntityGraph annotations, and - if present - the appropriate entity graph configuration is applied within SimpleJpaRepository.applyRepositoryMethodMetadata(…). Previously we mistakenly treated the @EntityGraph annotation as a Query annotation which triggered a query resolution process that tried to find a named or create a derived query and failed. Removed @QueryAnnotation from @EntityGraph, since it should only be used to mark store specific @Query annotations. Renamed Jpa21QueryCustomizer to Jpa21Utils. Moved Jpa21Utils to org.springframework.data.jpa.util to avoid potential dependency cycles. Added EclipseLink and OpenJPA specific subclasses for the EntityGraphRepositoryMethodsIntegrationTests to make sure the tests are executed for EclipseLink and OpenJPA as well. Original pull request: #109.
This commit is contained in:
committed by
Oliver Gierke
parent
6ea82fa1c5
commit
bacf6cce26
@@ -0,0 +1,27 @@
|
||||
/*
|
||||
* Copyright 2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.jpa.repository;
|
||||
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
|
||||
/**
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
@ContextConfiguration("classpath:eclipselink.xml")
|
||||
public class EclipseLinkEntityGraphRepositoryMethodsIntegrationTests extends
|
||||
EntityGraphRepositoryMethodsIntegrationTests {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
/*
|
||||
* Copyright 2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.jpa.repository;
|
||||
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.springframework.data.jpa.support.EntityManagerTestUtils.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.Persistence;
|
||||
|
||||
import org.junit.Assume;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.jpa.domain.sample.Role;
|
||||
import org.springframework.data.jpa.domain.sample.User;
|
||||
import org.springframework.data.jpa.repository.sample.RepositoryMethodsWithEntityGraphConfigJpaRepository;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
/**
|
||||
* @author Thomas Darimont
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration("classpath:config/namespace-autoconfig-context.xml")
|
||||
@Transactional
|
||||
public class EntityGraphRepositoryMethodsIntegrationTests {
|
||||
|
||||
@Autowired EntityManager em;
|
||||
@Autowired RepositoryMethodsWithEntityGraphConfigJpaRepository repository;
|
||||
|
||||
User tom;
|
||||
Role role;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
|
||||
tom = new User("Thomas", "Darimont", "tdarimont@example.org");
|
||||
role = new Role("Developer");
|
||||
em.persist(role);
|
||||
tom.getRoles().add(role);
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAJPA-612
|
||||
*/
|
||||
@Test
|
||||
public void shouldRespectConfiguredJpaEntityGraph() {
|
||||
|
||||
Assume.assumeTrue(currentEntityManagerIsAJpa21EntityManager(em));
|
||||
|
||||
tom = repository.save(tom);
|
||||
|
||||
List<User> result = repository.findAll();
|
||||
|
||||
assertThat(result.size(), is(1));
|
||||
assertThat(Persistence.getPersistenceUtil().isLoaded(result.get(0).getRoles()), is(true));
|
||||
assertThat(result.get(0), is(tom));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
* Copyright 2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.jpa.repository;
|
||||
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
|
||||
/**
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
@ContextConfiguration("classpath:openjpa.xml")
|
||||
public class OpenJpaEntityGraphRepositoryMethodsIntegrationTests extends EntityGraphRepositoryMethodsIntegrationTests {}
|
||||
@@ -17,6 +17,7 @@ package org.springframework.data.jpa.repository.query;
|
||||
|
||||
import static org.mockito.Matchers.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
import static org.springframework.data.jpa.support.EntityManagerTestUtils.*;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.List;
|
||||
@@ -43,12 +44,12 @@ import org.springframework.data.repository.core.support.DefaultRepositoryMetadat
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
|
||||
/**
|
||||
* Integration test for {@link AbstractJpaQuery}.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
* @author Thomas Darimont
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration("classpath:infrastructure.xml")
|
||||
@@ -133,7 +134,7 @@ public class AbstractJpaQueryTests {
|
||||
@Transactional
|
||||
public void shouldAddEntityGraphHintForFetch() throws Exception {
|
||||
|
||||
Assume.assumeTrue(currentEntityManagerIsAJpa21EntityManager());
|
||||
Assume.assumeTrue(currentEntityManagerIsAJpa21EntityManager(em));
|
||||
|
||||
Method findAllMethod = SampleRepository.class.getMethod("findAll");
|
||||
QueryExtractor provider = PersistenceProvider.fromEntityManager(em);
|
||||
@@ -155,7 +156,7 @@ public class AbstractJpaQueryTests {
|
||||
@Transactional
|
||||
public void shouldAddEntityGraphHintForLoad() throws Exception {
|
||||
|
||||
Assume.assumeTrue(currentEntityManagerIsAJpa21EntityManager());
|
||||
Assume.assumeTrue(currentEntityManagerIsAJpa21EntityManager(em));
|
||||
|
||||
Method getByIdMethod = SampleRepository.class.getMethod("getById", Integer.class);
|
||||
QueryExtractor provider = PersistenceProvider.fromEntityManager(em);
|
||||
@@ -170,11 +171,6 @@ public class AbstractJpaQueryTests {
|
||||
verify(result).setHint("javax.persistence.loadgraph", entityGraph);
|
||||
}
|
||||
|
||||
private boolean currentEntityManagerIsAJpa21EntityManager() {
|
||||
return ReflectionUtils.findMethod(((org.springframework.orm.jpa.EntityManagerProxy) em).getTargetEntityManager()
|
||||
.getClass(), "getEntityGraph", String.class) != null;
|
||||
}
|
||||
|
||||
interface SampleRepository extends Repository<User, Integer> {
|
||||
|
||||
@QueryHints({ @QueryHint(name = "foo", value = "bar") })
|
||||
|
||||
@@ -38,6 +38,7 @@ import org.springframework.data.domain.Sort;
|
||||
import org.springframework.data.jpa.domain.sample.User;
|
||||
import org.springframework.data.jpa.repository.EntityGraph;
|
||||
import org.springframework.data.jpa.repository.EntityGraph.EntityGraphType;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.Lock;
|
||||
import org.springframework.data.jpa.repository.Modifying;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
@@ -52,6 +53,7 @@ import org.springframework.data.repository.query.QueryMethod;
|
||||
* Unit test for {@link QueryMethod}.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
* @author Thomas Darimont
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class JpaQueryMethodUnitTests {
|
||||
@@ -329,6 +331,19 @@ public class JpaQueryMethodUnitTests {
|
||||
assertThat(method.getEntityGraph().getType(), is(EntityGraphType.LOAD));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAJPA-612
|
||||
*/
|
||||
@Test
|
||||
public void shouldFindEntityGraphAnnotationOnOverriddenSimpleJpaRepositoryMethod() throws Exception {
|
||||
|
||||
JpaQueryMethod method = new JpaQueryMethod(JpaRepositoryOverride.class.getMethod("findAll"), metadata, extractor);
|
||||
|
||||
assertThat(method.getEntityGraph(), is(notNullValue()));
|
||||
assertThat(method.getEntityGraph().getName(), is("User.detail"));
|
||||
assertThat(method.getEntityGraph().getType(), is(EntityGraphType.FETCH));
|
||||
}
|
||||
|
||||
/**
|
||||
* Interface to define invalid repository methods for testing.
|
||||
*
|
||||
@@ -391,6 +406,16 @@ public class JpaQueryMethodUnitTests {
|
||||
User queryMethodWithCustomEntityFetchGraph(Integer id);
|
||||
}
|
||||
|
||||
static interface JpaRepositoryOverride extends JpaRepository<User, Long> {
|
||||
|
||||
/**
|
||||
* DATAJPA-612
|
||||
*/
|
||||
@Override
|
||||
@EntityGraph("User.detail")
|
||||
public List<User> findAll();
|
||||
}
|
||||
|
||||
@Lock(LockModeType.OPTIMISTIC_FORCE_INCREMENT)
|
||||
@QueryHints(@QueryHint(name = "foo", value = "bar"))
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
* Copyright 2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.jpa.repository.sample;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.data.jpa.domain.sample.User;
|
||||
import org.springframework.data.jpa.repository.EntityGraph;
|
||||
import org.springframework.data.jpa.repository.EntityGraph.EntityGraphType;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
/**
|
||||
* Custom repository interface that customizes the fetching behavior of querys of well known repository interface methods via {@link EntityGraph}
|
||||
* annotation.
|
||||
*
|
||||
* @author Thomas Darimont
|
||||
*/
|
||||
public interface RepositoryMethodsWithEntityGraphConfigJpaRepository extends JpaRepository<User, Long> {
|
||||
|
||||
/**
|
||||
* Should find all users.
|
||||
*/
|
||||
@EntityGraph(type = EntityGraphType.LOAD, value = "User.overview")
|
||||
List<User> findAll();
|
||||
}
|
||||
Reference in New Issue
Block a user