Render HQL Boolean literals correctly on dialects which use BIT for boolean

This commit is contained in:
gavinking 2020-01-30 19:41:36 +01:00 committed by Steve Ebersole
parent 0d3b130258
commit 9a061c5c10
1 changed files with 20 additions and 0 deletions

View File

@ -12,11 +12,14 @@ import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import org.hibernate.dialect.Dialect;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.type.descriptor.ValueBinder;
import org.hibernate.type.descriptor.ValueExtractor;
import org.hibernate.type.descriptor.WrapperOptions;
import org.hibernate.type.descriptor.java.BasicJavaDescriptor;
import org.hibernate.type.descriptor.java.JavaTypeDescriptor;
import org.hibernate.type.descriptor.sql.spi.BasicJdbcLiteralFormatter;
import org.hibernate.type.spi.TypeConfiguration;
/**
@ -47,6 +50,23 @@ public class BitTypeDescriptor implements SqlTypeDescriptor {
return (BasicJavaDescriptor<T>) typeConfiguration.getJavaTypeDescriptorRegistry().getDescriptor( Boolean.class );
}
public <T> JdbcLiteralFormatter<T> getJdbcLiteralFormatter(JavaTypeDescriptor<T> javaTypeDescriptor) {
if ( javaTypeDescriptor.getJavaType().equals(Boolean.class) ) {
//this is to allow literals to be formatted correctly when
//we are in the legacy Boolean-to-BIT JDBC type mapping mode
return new BasicJdbcLiteralFormatter( javaTypeDescriptor ) {
@Override
public String toJdbcLiteral(Object value, Dialect dialect, SharedSessionContractImplementor session) {
Boolean bool = unwrap( value, Boolean.class, session );
return bool ? "1" : "0";
}
};
}
else {
return (value, dialect, session) -> value.toString();
}
}
@Override
public <X> ValueBinder<X> getBinder(final JavaTypeDescriptor<X> javaTypeDescriptor) {
return new BasicBinder<X>( javaTypeDescriptor, this ) {