From e3cc2d78e94dc1fb446a601be5a379f00f142a41 Mon Sep 17 00:00:00 2001 From: Chris Cranford Date: Mon, 11 Dec 2017 12:08:38 -0500 Subject: [PATCH] HHH-12119 - Fix NameQueryTupleTransformer discarding alias name case. --- .../jpa/spi/NativeQueryTupleTransformer.java | 12 ++++++----- .../jpa/test/query/TupleNativeQueryTest.java | 20 +++++++++---------- 2 files changed, 17 insertions(+), 15 deletions(-) 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 397933cec9..c68fe35d73 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 @@ -55,6 +55,7 @@ public class NativeQueryTupleTransformer extends BasicTransformerAdapter { private Object[] tuple; private Map aliasToValue = new LinkedHashMap<>(); + private Map aliasReferences = new LinkedHashMap<>(); public NativeTupleImpl(Object[] tuple, String[] aliases) { if ( tuple == null ) { @@ -68,7 +69,8 @@ public class NativeQueryTupleTransformer extends BasicTransformerAdapter { } this.tuple = tuple; for ( int i = 0; i < tuple.length; i++ ) { - aliasToValue.put( aliases[i].toLowerCase(), tuple[i] ); + aliasToValue.put( aliases[i], tuple[i] ); + aliasReferences.put( aliases[i].toLowerCase(), aliases[i] ); } } @@ -81,11 +83,11 @@ public class NativeQueryTupleTransformer extends BasicTransformerAdapter { @Override public Object get(String alias) { - final String lowerCasedAlias = alias.toLowerCase(); - if (!aliasToValue.containsKey(lowerCasedAlias)) { - throw new IllegalArgumentException( "Unknown alias [" + alias + "]" ); + final String aliasReference = aliasReferences.get( alias.toLowerCase() ); + if ( aliasReference != null && aliasToValue.containsKey( aliasReference ) ) { + return aliasToValue.get( aliasReference ); } - return aliasToValue.get(lowerCasedAlias); + throw new IllegalArgumentException( "Unknown alias [" + alias + "]" ); } @Override 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 a7426aecdf..25eb4f514f 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 @@ -200,9 +200,9 @@ public class TupleNativeQueryTest extends BaseEntityManagerFunctionalTestCase { List> result = tuples.get(0).getElements(); assertEquals(2, result.size()); assertEquals(BigInteger.class, result.get(0).getJavaType()); - assertEquals("id", result.get(0).getAlias()); + assertEquals("ID", result.get(0).getAlias()); assertEquals(String.class, result.get(1).getJavaType()); - assertEquals("firstname", result.get(1).getAlias()); + assertEquals("FIRSTNAME", result.get(1).getAlias()); }); } @@ -362,9 +362,9 @@ public class TupleNativeQueryTest extends BaseEntityManagerFunctionalTestCase { List> result = tuples.get(0).getElements(); assertEquals(2, result.size()); assertEquals(BigInteger.class, result.get(0).getJavaType()); - assertEquals("id", result.get(0).getAlias()); + assertEquals("ID", result.get(0).getAlias()); assertEquals(String.class, result.get(1).getJavaType()); - assertEquals("firstname", result.get(1).getAlias()); + assertEquals("FIRSTNAME", result.get(1).getAlias()); }); } @@ -524,9 +524,9 @@ public class TupleNativeQueryTest extends BaseEntityManagerFunctionalTestCase { List> result = tuples.get(0).getElements(); assertEquals(2, result.size()); assertEquals(BigInteger.class, result.get(0).getJavaType()); - assertEquals("id", result.get(0).getAlias()); + assertEquals("ID", result.get(0).getAlias()); assertEquals(String.class, result.get(1).getJavaType()); - assertEquals("firstname", result.get(1).getAlias()); + assertEquals("FIRSTNAME", result.get(1).getAlias()); }); } @@ -686,9 +686,9 @@ public class TupleNativeQueryTest extends BaseEntityManagerFunctionalTestCase { List> result = tuples.get(0).getElements(); assertEquals(2, result.size()); assertEquals(BigInteger.class, result.get(0).getJavaType()); - assertEquals("id", result.get(0).getAlias()); + assertEquals("ID", result.get(0).getAlias()); assertEquals(String.class, result.get(1).getJavaType()); - assertEquals("firstname", result.get(1).getAlias()); + assertEquals("FIRSTNAME", result.get(1).getAlias()); }); } @@ -706,11 +706,11 @@ public class TupleNativeQueryTest extends BaseEntityManagerFunctionalTestCase { assertEquals(2, result.size()); final TupleElement firstTupleElement = result.get(0); assertEquals(BigInteger.class, firstTupleElement.getJavaType()); - assertEquals("id", firstTupleElement.getAlias()); + 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()); + assertEquals("FIRSTNAME", secondTupleElement.getAlias()); assertNull(tuple.get(secondTupleElement.getAlias())); }); }