HHH-15454 correct get(alias, class) method in TupleImpl

This commit is contained in:
KARGET 2022-08-24 11:24:22 +02:00 committed by Christian Beikov
parent 70e28876a6
commit 254d69568b
1 changed files with 9 additions and 2 deletions

View File

@ -9,6 +9,7 @@ package org.hibernate.sql.results.internal;
import java.util.List; import java.util.List;
import jakarta.persistence.TupleElement; import jakarta.persistence.TupleElement;
import org.hibernate.internal.util.type.PrimitiveWrapperHelper;
import org.hibernate.query.JpaTuple; import org.hibernate.query.JpaTuple;
/** /**
@ -43,7 +44,7 @@ public class TupleImpl implements JpaTuple {
public <X> X get(String alias, Class<X> type) { public <X> X get(String alias, Class<X> type) {
final Object untyped = get( alias ); final Object untyped = get( alias );
if ( untyped != null ) { if ( untyped != null ) {
if ( !type.isInstance( untyped ) ) { if (!elementTypeMatches(type, untyped)) {
throw new IllegalArgumentException( throw new IllegalArgumentException(
String.format( String.format(
"Requested tuple value [alias=%s, value=%s] cannot be assigned to requested type [%s]", "Requested tuple value [alias=%s, value=%s] cannot be assigned to requested type [%s]",
@ -73,7 +74,7 @@ public class TupleImpl implements JpaTuple {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public <X> X get(int i, Class<X> type) { public <X> X get(int i, Class<X> type) {
final Object result = get( i ); final Object result = get( i );
if ( result != null && !type.isInstance( result ) ) { if ( result != null && !elementTypeMatches( type, result ) ) {
throw new IllegalArgumentException( throw new IllegalArgumentException(
String.format( String.format(
"Requested tuple value [index=%s, realType=%s] cannot be assigned to requested type [%s]", "Requested tuple value [index=%s, realType=%s] cannot be assigned to requested type [%s]",
@ -96,6 +97,12 @@ public class TupleImpl implements JpaTuple {
return row[i]; return row[i];
} }
private <X> boolean elementTypeMatches(Class<X> type, Object untyped) {
return type.isInstance(untyped)
|| type.isPrimitive()
&& PrimitiveWrapperHelper.getDescriptorByPrimitiveType( type).getWrapperClass().isInstance( untyped);
}
@Override @Override
public Object[] toArray() { public Object[] toArray() {
return row; return row;