DATAJPA-885 - Tuple queries are now only issued for non-JPA managed types.

We now explicitly check the return types for queries using manually defined queries to find out whether it's a JPA managed type in the first place. Only if that's not the case we resort to a Tuple query and assume DTO mapping happening downstream.

This is necessary as a singular projection expression in the query might simply return an element of the aggregate and thus doesn't need any DTO creation. In case an unmanaged type is returned from the query method we assume DTO creation.
This commit is contained in:
Oliver Gierke
2016-04-09 15:41:03 +02:00
parent c5ac2baa50
commit d64494ade6
2 changed files with 108 additions and 1 deletions

View File

@@ -18,6 +18,7 @@ package org.springframework.data.jpa.repository.query;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import javax.persistence.Tuple;
import javax.persistence.metamodel.ManagedType;
import org.springframework.data.repository.query.EvaluationContextProvider;
import org.springframework.data.repository.query.ParameterAccessor;
@@ -137,6 +138,25 @@ abstract class AbstractStringBasedJpaQuery extends AbstractJpaQuery {
ResultProcessor resultFactory = getQueryMethod().getResultProcessor();
ReturnedType returnedType = resultFactory.getReturnedType();
return returnedType.isProjecting() ? em.createQuery(queryString, Tuple.class) : em.createQuery(queryString);
return returnedType.isProjecting() && !isJpaManaged(returnedType.getReturnedType(), em)
? em.createQuery(queryString, Tuple.class) : em.createQuery(queryString);
}
/**
* Returns whether the given type is managed by the given {@link EntityManager}
* @param type must not be {@literal null}.
* @param em must not be {@literal null}.
*
* @return
*/
private static boolean isJpaManaged(Class<?> type, EntityManager em) {
for (ManagedType<?> managedType : em.getMetamodel().getManagedTypes()) {
if (managedType.getJavaType().equals(type)) {
return true;
}
}
return false;
}
}

View File

@@ -0,0 +1,87 @@
/*
* Copyright 2016 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.query;
import static org.mockito.Matchers.*;
import static org.mockito.Mockito.*;
import java.lang.reflect.Method;
import java.util.Set;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Tuple;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.data.jpa.domain.sample.Role;
import org.springframework.data.jpa.domain.sample.User;
import org.springframework.data.jpa.provider.PersistenceProvider;
import org.springframework.data.projection.SpelAwareProxyProjectionFactory;
import org.springframework.data.repository.Repository;
import org.springframework.data.repository.core.support.DefaultRepositoryMetadata;
import org.springframework.data.repository.query.DefaultEvaluationContextProvider;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* Integration tests for {@link AbstractStringBasedJpaQuery}.
*
* @author Oliver Gierke
* @soundtrack Henrik Freischlader Trio - Nobody Else To Blame (Openness)
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:infrastructure.xml")
public class AbstractStringBasedJpaQueryIntegrationTests {
@PersistenceContext EntityManager em;
/**
* @see DATAJPA-885
*/
@Test
public void createsNormalQueryForJpaManagedReturnTypes() throws Exception {
EntityManager mock = mock(EntityManager.class);
when(mock.getEntityManagerFactory()).thenReturn(em.getEntityManagerFactory());
when(mock.getMetamodel()).thenReturn(em.getMetamodel());
JpaQueryMethod method = getMethod("findRolesByEmailAddress", String.class);
AbstractStringBasedJpaQuery jpaQuery = new SimpleJpaQuery(method, mock, DefaultEvaluationContextProvider.INSTANCE,
new SpelExpressionParser());
jpaQuery.createJpaQuery(method.getAnnotatedQuery());
verify(mock, times(1)).createQuery(anyString());
verify(mock, times(0)).createQuery(anyString(), eq(Tuple.class));
}
private JpaQueryMethod getMethod(String name, Class<?>... parameterTypes) throws Exception {
Method method = SampleRepository.class.getMethod(name, parameterTypes);
PersistenceProvider persistenceProvider = PersistenceProvider.fromEntityManager(em);
return new JpaQueryMethod(method, new DefaultRepositoryMetadata(SampleRepository.class),
new SpelAwareProxyProjectionFactory(), persistenceProvider);
}
interface SampleRepository extends Repository<User, Integer> {
@org.springframework.data.jpa.repository.Query("select u.roles from User u where u.emailAddress = ?1")
Set<Role> findRolesByEmailAddress(String emailAddress);
}
}