HHH-12067: NativeQueryTupleTransformer doesn't handle null values in returned ResultSet

This commit is contained in:
msymonov 2017-10-28 11:52:07 +03:00 committed by Vlad Mihalcea
parent c3f61c73dd
commit 4b8cfe0c1f
3 changed files with 49 additions and 9 deletions

View File

@ -81,12 +81,11 @@ public class NativeQueryTupleTransformer extends BasicTransformerAdapter {
@Override
public Object get(String alias) {
Object tupleElement = aliasToValue.get( alias.toLowerCase() );
if ( tupleElement == null ) {
final String lowerCasedAlias = alias.toLowerCase();
if (!aliasToValue.containsKey(lowerCasedAlias)) {
throw new IllegalArgumentException( "Unknown alias [" + alias + "]" );
}
return tupleElement;
return aliasToValue.get(lowerCasedAlias);
}
@Override

View File

@ -0,0 +1,35 @@
package org.hibernate.jpa.spi;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import javax.persistence.Tuple;
import org.junit.Test;
/**
* @author Maksym Symonov
*/
public class NativeQueryTupleTransformerTest {
private final NativeQueryTupleTransformer nativeQueryTupleTransformer = new NativeQueryTupleTransformer();
@Test
public void nullValueIsExtractedFromTuple() {
final Tuple tuple = (Tuple) nativeQueryTupleTransformer.transformTuple(
new Object[] { 1L, null },
new String[] { "id", "value" }
);
assertEquals(1L, tuple.get("id"));
assertNull(tuple.get("value"));
}
@Test(expected = IllegalArgumentException.class)
public void missingAliasCausesExceptionWhenIsExtractedFromTuple() {
final Tuple tuple = (Tuple) nativeQueryTupleTransformer.transformTuple(
new Object[] { 1L, null },
new String[] { "id", "value" }
);
tuple.get("unknownAlias");
}
}

View File

@ -18,6 +18,7 @@ import java.util.stream.Collectors;
import static org.hibernate.testing.transaction.TransactionUtil.doInJPA;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
@RequiresDialect(H2Dialect.class)
public class TupleNativeQueryTest extends BaseEntityManagerFunctionalTestCase {
@ -700,12 +701,17 @@ public class TupleNativeQueryTest extends BaseEntityManagerFunctionalTestCase {
});
doInJPA(this::entityManagerFactory, entityManager -> {
List<Tuple> tuples = getTupleResult(entityManager);
List<TupleElement<?>> result = tuples.get(0).getElements();
final Tuple tuple = tuples.get(0);
List<TupleElement<?>> result = tuple.getElements();
assertEquals(2, result.size());
assertEquals(BigInteger.class, result.get(0).getJavaType());
assertEquals("id", result.get(0).getAlias());
assertEquals(Object.class, result.get(1).getJavaType());
assertEquals("firstname", result.get(1).getAlias());
final TupleElement<?> firstTupleElement = result.get(0);
assertEquals(BigInteger.class, firstTupleElement.getJavaType());
assertEquals("id", firstTupleElement.getAlias());
assertEquals(BigInteger.valueOf(1L), tuple.get(firstTupleElement.getAlias()));
final TupleElement<?> secondTupleElement = result.get(1);
assertEquals(Object.class, secondTupleElement.getJavaType());
assertEquals("firstname", secondTupleElement.getAlias());
assertNull(tuple.get(secondTupleElement.getAlias()));
});
}