diff --git a/src/main/java/org/springframework/data/jpa/repository/query/AbstractJpaQuery.java b/src/main/java/org/springframework/data/jpa/repository/query/AbstractJpaQuery.java index 4b7c7b6ac..780725e33 100644 --- a/src/main/java/org/springframework/data/jpa/repository/query/AbstractJpaQuery.java +++ b/src/main/java/org/springframework/data/jpa/repository/query/AbstractJpaQuery.java @@ -16,6 +16,7 @@ package org.springframework.data.jpa.repository.query; import java.util.HashMap; +import java.util.List; import java.util.Map; import javax.persistence.EntityManager; @@ -39,6 +40,7 @@ import org.springframework.data.jpa.util.JpaMetamodel; import org.springframework.data.repository.query.ParametersParameterAccessor; import org.springframework.data.repository.query.RepositoryQuery; import org.springframework.data.repository.query.ResultProcessor; +import org.springframework.data.repository.query.ReturnedType; import org.springframework.util.Assert; /** @@ -116,7 +118,7 @@ public abstract class AbstractJpaQuery implements RepositoryQuery { ParametersParameterAccessor accessor = new ParametersParameterAccessor(method.getParameters(), values); ResultProcessor withDynamicProjection = method.getResultProcessor().withDynamicProjection(accessor); - return withDynamicProjection.processResult(result, TupleConverter.INSTANCE); + return withDynamicProjection.processResult(result, new TupleConverter(withDynamicProjection.getReturnedType())); } protected JpaQueryExecution getExecution() { @@ -232,9 +234,21 @@ public abstract class AbstractJpaQuery implements RepositoryQuery { */ protected abstract Query doCreateCountQuery(Object[] values); - private static enum TupleConverter implements Converter { + static class TupleConverter implements Converter { - INSTANCE; + private final ReturnedType type; + + /** + * Creates a new {@link TupleConverter} for the given {@link ReturnedType}. + * + * @param type must not be {@literal null}. + */ + public TupleConverter(ReturnedType type) { + + Assert.notNull(type, "Returned type must not be null!"); + + this.type = type; + } /* * (non-Javadoc) @@ -249,8 +263,18 @@ public abstract class AbstractJpaQuery implements RepositoryQuery { Tuple tuple = (Tuple) source; Map result = new HashMap(); + List> elements = tuple.getElements(); - for (TupleElement element : tuple.getElements()) { + if (elements.size() == 1) { + + Object value = tuple.get(elements.get(0)); + + if (type.isInstance(value)) { + return value; + } + } + + for (TupleElement element : elements) { String alias = element.getAlias(); diff --git a/src/test/java/org/springframework/data/jpa/repository/query/TupleConverterUnitTests.java b/src/test/java/org/springframework/data/jpa/repository/query/TupleConverterUnitTests.java new file mode 100644 index 000000000..6e1ee977b --- /dev/null +++ b/src/test/java/org/springframework/data/jpa/repository/query/TupleConverterUnitTests.java @@ -0,0 +1,75 @@ +/* + * 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.hamcrest.CoreMatchers.*; +import static org.junit.Assert.*; +import static org.mockito.Mockito.*; + +import java.util.Arrays; + +import javax.persistence.Tuple; +import javax.persistence.TupleElement; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.runners.MockitoJUnitRunner; +import org.springframework.data.jpa.repository.query.AbstractJpaQuery.TupleConverter; +import org.springframework.data.projection.ProjectionFactory; +import org.springframework.data.repository.CrudRepository; +import org.springframework.data.repository.core.RepositoryMetadata; +import org.springframework.data.repository.core.support.DefaultRepositoryMetadata; +import org.springframework.data.repository.query.QueryMethod; +import org.springframework.data.repository.query.ReturnedType; + +/** + * Unit tests for {@link TupleConverter}. + * + * @author Oliver Gierke + * @soundtrack James Bay - Let it go (Chaos and the Calm) + */ +@RunWith(MockitoJUnitRunner.class) +public class TupleConverterUnitTests { + + @Mock Tuple tuple; + @Mock TupleElement element; + @Mock ProjectionFactory factory; + + /** + * @see DATAJPA-984 + */ + @Test + @SuppressWarnings("unchecked") + public void returnsSingleTupleElementIfItMatchesExpectedType() throws Exception { + + RepositoryMetadata metadata = new DefaultRepositoryMetadata(SampleRepository.class); + QueryMethod method = new QueryMethod(SampleRepository.class.getMethod("someMethod"), metadata, factory); + ReturnedType type = method.getResultProcessor().getReturnedType(); + + doReturn(element).when(tuple).get(0); + doReturn(Arrays.asList(element)).when(tuple).getElements(); + doReturn("Foo").when(tuple).get(element); + + TupleConverter converter = new TupleConverter(type); + + assertThat(converter.convert(tuple), is((Object) "Foo")); + } + + static interface SampleRepository extends CrudRepository { + String someMethod(); + } +}