From 50b835b145b24424cd47e6bea3ac51d38e0f9b2c Mon Sep 17 00:00:00 2001 From: Andrea Boriero Date: Thu, 9 Feb 2023 12:13:40 +0100 Subject: [PATCH] HHH-16119 Named native queries do not work with jakarta.persistence.Tuple result class --- .../jpa/spi/NativeQueryTupleTransformer.java | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 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 440082fc42..7634dd5a42 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 @@ -60,6 +60,8 @@ public class NativeQueryTupleTransformer implements ResultTransformer, Ty private final Object[] tuple; + private final int size; + private final Map aliasToValue = new LinkedHashMap<>(); private final Map aliasReferences = new LinkedHashMap<>(); @@ -75,9 +77,13 @@ public class NativeQueryTupleTransformer implements ResultTransformer, Ty } this.tuple = tuple; for ( int i = 0; i < tuple.length; i++ ) { - aliasToValue.put( aliases[i], tuple[i] ); - aliasReferences.put( aliases[i].toLowerCase(), aliases[i] ); + final String alias = aliases[i]; + if ( alias != null ) { + aliasToValue.put( alias, tuple[i] ); + aliasReferences.put( alias.toLowerCase(), alias ); + } } + size = tuple.length; } @Override @@ -108,7 +114,7 @@ public class NativeQueryTupleTransformer implements ResultTransformer, Ty if ( i < 0 ) { throw new IllegalArgumentException( "requested tuple index must be greater than zero" ); } - if ( i >= aliasToValue.size() ) { + if ( i >= size ) { throw new IllegalArgumentException( "requested tuple index exceeds actual tuple size" ); } return tuple[i]; @@ -122,7 +128,7 @@ public class NativeQueryTupleTransformer implements ResultTransformer, Ty @Override public List> getElements() { - List> elements = new ArrayList<>( aliasToValue.size() ); + List> elements = new ArrayList<>( size ); for ( Map.Entry entry : aliasToValue.entrySet() ) { elements.add( new NativeTupleElementImpl<>( getValueClass( entry.getValue() ), entry.getKey() ) );