I use Spring-orm 4.3.30 . At Hibernate 5.1.17, class createSqlQuery use AbstractQueryImpl and we can use Custom Type with prepareStatement
for example :
prepareStatement.setParameter(2, reportingYear.getEndMonth(), YearMonthType.class)
AbstractQueryImpl :
public Query setParameter(int position, Object val, Type type) {
if ( parameterMetadata.getOrdinalParameterCount() == 0 ) {
throw new IllegalArgumentException( "No positional parameters in query: " + getQueryString() );
}
if ( position < 0 || position > parameterMetadata.getOrdinalParameterCount() - 1 ) {
throw new IllegalArgumentException( "Positional parameter does not exist: " + position + " in query: " + getQueryString() );
}
int size = values.size();
if ( position < size ) {
values.set( position, val );
types.set( position, type );
}
else {
// prepend value and type list with null for any positions before the wanted position.
for ( int i = 0; i < position - size; i++ ) {
values.add( UNSET_PARAMETER );
types.add( UNSET_TYPE );
}
values.add( val );
**types.add( type );**
}
return this;
}
Since Hibernate 5.2 it use : NativeQueryImpl
and when we use prepareStatement with custom type.
NativeQueryImpl try to resolve type custom with SessionFactoryImpl.resolveParameterBindType
@Override
public Type resolveParameterBindType(Class clazz){
String typename = clazz.getName();
Type type = getTypeResolver().heuristicType( typename );
boolean serializable = type != null && type instanceof SerializableType;
if ( type == null || serializable ) {
try {
getMetamodel().entityPersister( clazz.getName() );
}
catch (MappingException me) {
if ( serializable ) {
return type;
}
else {
throw new HibernateException( "Could not determine a type for class: " + typename );
}
}
return getTypeHelper().entity( clazz );
}
else {
return type;
}
}
when call :
Type type = getTypeResolver().heuristicType( typename );
there was no custom type in TypeResolver.
/**
* Retrieve the {@link Type} resolver associated with this factory.
*
* @return The type resolver
*
* @deprecated (since 5.3) No replacement, access to and handling of Types will be much different in 6.0
*/
@Deprecated
public TypeResolver getTypeResolver() {
return metamodel.getTypeConfiguration().getTypeResolver();
}
Knowing that @TypDef not work.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.tennaxia.t3.reporting.bo">
<typedef name="YearMonth" class="com.tennaxia.t3.common.date.YearMonthType" />
Knowing that With JPQL, the custom type works with setParameter.
How register my type custom and can be used with Sql native query ?
Another solution is to add custom type in QueryParameterBindingsImpl.parameterMetadata but i search again how add this custom type in this parameterMetadata.
thanks a lot to help me to understand this.
You can set the parameter with the
org.hibernate.engine.spi.TypedValuewrapper which also includes a type.