HHH-17355 Support binding single element value for basic plural parameter types

This commit is contained in:
Christian Beikov 2023-10-26 18:09:38 +02:00
parent e8518a6073
commit c257be699a
4 changed files with 57 additions and 0 deletions

View File

@ -14,6 +14,7 @@ import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.query.BindableType;
import org.hibernate.query.QueryArgumentException;
import org.hibernate.query.sqm.SqmExpressible;
import org.hibernate.type.descriptor.java.BasicPluralJavaType;
import org.hibernate.type.descriptor.java.JavaType;
import jakarta.persistence.TemporalType;
@ -141,6 +142,13 @@ public class QueryParameterBindingValidator {
return parameterDeclarationIsTemporal && bindIsTemporal;
}
// Allow binding a single element for a basic plural parameter type
else if ( expectedJavaType instanceof BasicPluralJavaType<?> ) {
final JavaType<?> elementJavaType = ( (BasicPluralJavaType<?>) expectedJavaType ).getElementJavaType();
if ( elementJavaType.isInstance( value ) ) {
return true;
}
}
return false;
}

View File

@ -6,8 +6,10 @@
*/
package org.hibernate.type;
import java.lang.reflect.Array;
import java.util.Objects;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.type.descriptor.java.JavaType;
import org.hibernate.type.descriptor.jdbc.JdbcType;
import org.hibernate.type.descriptor.jdbc.JdbcTypeIndicators;
@ -54,6 +56,20 @@ public class BasicArrayType<T,E>
return (BasicType<X>) this;
}
@Override
public Object disassemble(Object value, SharedSessionContractImplementor session) {
if ( value == null ) {
return null;
}
if ( baseDescriptor.isInstance( (E) value ) ) {
// Support binding a single element as parameter value
final Object array = Array.newInstance( baseDescriptor.getJavaType(), 1 );
Array.set( array, 0, value );
return array;
}
return value;
}
@Override
public boolean equals(Object o) {
return o == this || o.getClass() == BasicArrayType.class

View File

@ -6,9 +6,11 @@
*/
package org.hibernate.type;
import java.lang.reflect.Array;
import java.util.Collection;
import java.util.Objects;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.type.descriptor.java.JavaType;
import org.hibernate.type.descriptor.java.spi.BasicCollectionJavaType;
import org.hibernate.type.descriptor.jdbc.JdbcType;
@ -75,6 +77,21 @@ public class BasicCollectionType<C extends Collection<E>, E>
return (BasicType<X>) this;
}
@Override
public Object disassemble(Object value, SharedSessionContractImplementor session) {
if ( value == null ) {
return null;
}
if ( baseDescriptor.isInstance( (E) value ) ) {
// Support binding a single element as parameter value
final BasicCollectionJavaType<C, E> javaType = (BasicCollectionJavaType<C, E>) getJavaTypeDescriptor();
final C collection = javaType.getSemantics().instantiateRaw( 1, null );
collection.add( (E) value );
return collection;
}
return value;
}
@Override
public boolean equals(Object o) {
return o == this || o.getClass() == BasicCollectionType.class

View File

@ -6,8 +6,10 @@
*/
package org.hibernate.type;
import java.lang.reflect.Array;
import java.util.Objects;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.type.descriptor.ValueBinder;
import org.hibernate.type.descriptor.ValueExtractor;
import org.hibernate.type.descriptor.converter.spi.BasicValueConverter;
@ -76,6 +78,20 @@ public class ConvertedBasicArrayType<T,S,E>
return (BasicType<X>) this;
}
@Override
public Object disassemble(Object value, SharedSessionContractImplementor session) {
if ( value == null ) {
return null;
}
if ( baseDescriptor.isInstance( (E) value ) ) {
// Support binding a single element as parameter value
final Object array = Array.newInstance( baseDescriptor.getJavaType(), 1 );
Array.set( array, 0, value );
return array;
}
return value;
}
@Override
public BasicValueConverter<T, ?> getValueConverter() {
return converter;