DATAJPA-1024 - TupleConverter now guards against single-element tuples with null value.

We now also return a single-element tuple value as is if it's null as for some reason some persistence providers (*cough* Hibernate *cough*) will return a single-element, null value containing tuple (instead of null in the first place) for queries that didn't yield a result.
This commit is contained in:
Oliver Gierke
2016-12-13 10:46:44 +01:00
parent bf6a517cd0
commit 343f5c7228
2 changed files with 28 additions and 6 deletions

View File

@@ -269,7 +269,7 @@ public abstract class AbstractJpaQuery implements RepositoryQuery {
Object value = tuple.get(elements.get(0));
if (type.isInstance(value)) {
if (type.isInstance(value) || value == null) {
return value;
}
}

View File

@@ -24,6 +24,7 @@ import java.util.Arrays;
import javax.persistence.Tuple;
import javax.persistence.TupleElement;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
@@ -49,6 +50,17 @@ public class TupleConverterUnitTests {
@Mock TupleElement<String> element;
@Mock ProjectionFactory factory;
ReturnedType type;
@Before
public void setUp() throws Exception {
RepositoryMetadata metadata = new DefaultRepositoryMetadata(SampleRepository.class);
QueryMethod method = new QueryMethod(SampleRepository.class.getMethod("someMethod"), metadata, factory);
this.type = method.getResultProcessor().getReturnedType();
}
/**
* @see DATAJPA-984
*/
@@ -56,11 +68,6 @@ public class TupleConverterUnitTests {
@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);
@@ -69,6 +76,21 @@ public class TupleConverterUnitTests {
assertThat(converter.convert(tuple), is((Object) "Foo"));
}
/**
* @see DATAJPA-1024
*/
@Test
@SuppressWarnings("unchecked")
public void returnsNullForSingleElementTupleWithNullValue() throws Exception {
doReturn(Arrays.asList(element)).when(tuple).getElements();
doReturn(null).when(tuple).get(element);
TupleConverter converter = new TupleConverter(type);
assertThat(converter.convert(tuple), is(nullValue()));
}
static interface SampleRepository extends CrudRepository<Object, Long> {
String someMethod();
}