diff --git a/hibernate-core/src/main/java/org/hibernate/jpa/spi/NativeQueryTupleTransformer.java b/hibernate-core/src/main/java/org/hibernate/jpa/spi/NativeQueryTupleTransformer.java index c9cd55a7d1..397933cec9 100644 --- a/hibernate-core/src/main/java/org/hibernate/jpa/spi/NativeQueryTupleTransformer.java +++ b/hibernate-core/src/main/java/org/hibernate/jpa/spi/NativeQueryTupleTransformer.java @@ -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 diff --git a/hibernate-core/src/test/java/org/hibernate/jpa/spi/NativeQueryTupleTransformerTest.java b/hibernate-core/src/test/java/org/hibernate/jpa/spi/NativeQueryTupleTransformerTest.java new file mode 100644 index 0000000000..0def3e165a --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/jpa/spi/NativeQueryTupleTransformerTest.java @@ -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"); + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/jpa/test/query/TupleNativeQueryTest.java b/hibernate-core/src/test/java/org/hibernate/jpa/test/query/TupleNativeQueryTest.java index 449ce492b9..a7426aecdf 100644 --- a/hibernate-core/src/test/java/org/hibernate/jpa/test/query/TupleNativeQueryTest.java +++ b/hibernate-core/src/test/java/org/hibernate/jpa/test/query/TupleNativeQueryTest.java @@ -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 tuples = getTupleResult(entityManager); - List> result = tuples.get(0).getElements(); + final Tuple tuple = tuples.get(0); + List> 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())); }); }