HHH-468 - MysqlDialect incorrectly maps java.lang.Boolean to SQL BIT

This commit is contained in:
Steve Ebersole 2011-04-14 15:05:05 -05:00
parent 0023dc695d
commit d8fd8cd03b
3 changed files with 102 additions and 8 deletions

View File

@ -144,10 +144,40 @@ public abstract class Dialect {
registerFunction( "str", new SQLFunctionTemplate(StandardBasicTypes.STRING, "cast(?1 as char)") );
registerColumnType( Types.BIT, "bit" );
registerColumnType( Types.BOOLEAN, "boolean" );
registerColumnType( Types.TINYINT, "tinyint" );
registerColumnType( Types.SMALLINT, "smallint" );
registerColumnType( Types.INTEGER, "integer" );
registerColumnType( Types.BIGINT, "bigint" );
registerColumnType( Types.FLOAT, "float($p)" );
registerColumnType( Types.DOUBLE, "double precision" );
registerColumnType( Types.NUMERIC, "numeric($p,$s)" );
registerColumnType( Types.REAL, "real" );
registerColumnType( Types.DATE, "date" );
registerColumnType( Types.TIME, "time" );
registerColumnType( Types.TIMESTAMP, "timestamp" );
registerColumnType( Types.VARBINARY, "bit varying($l)" );
registerColumnType( Types.LONGVARBINARY, "bit varying($l)" );
registerColumnType( Types.BLOB, "blob" );
registerColumnType( Types.CHAR, "char($l)" );
registerColumnType( Types.VARCHAR, "varchar($l)" );
registerColumnType( Types.LONGVARCHAR, "varchar($l)" );
registerColumnType( Types.CLOB, "clob" );
registerColumnType( Types.NCHAR, "nchar($l)" );
registerColumnType( Types.NVARCHAR, "nvarchar($l)" );
registerColumnType( Types.LONGNVARCHAR, "nvarchar($l)" );
registerColumnType( Types.NCLOB, "nclob" );
// register hibernate types for default use in scalar sqlquery type auto detection
registerHibernateType( Types.BIGINT, StandardBasicTypes.BIG_INTEGER.getName() );
registerHibernateType( Types.BINARY, StandardBasicTypes.BINARY.getName() );
registerHibernateType( Types.BIT, StandardBasicTypes.BOOLEAN.getName() );
registerHibernateType( Types.BOOLEAN, StandardBasicTypes.BOOLEAN.getName() );
registerHibernateType( Types.CHAR, StandardBasicTypes.CHARACTER.getName() );
registerHibernateType( Types.DATE, StandardBasicTypes.DATE.getName() );
registerHibernateType( Types.DOUBLE, StandardBasicTypes.DOUBLE.getName() );
@ -320,9 +350,7 @@ public abstract class Dialect {
* if there is no override, then <code>sqlTypeDescriptor</code> is returned.
* @throws IllegalArgumentException if <code>sqlTypeDescriptor</code> is null.
*
* @see {@link SqlTypeDescriptor}
* @see {@link #getSqlTypeDescriptorOverride}
* @see {@link StandardBasicTypes#isStandardBasicSqlTypeDescriptor(org.hibernate.type.descriptor.sql.SqlTypeDescriptor)}
*/
public SqlTypeDescriptor remapSqlTypeDescriptor(SqlTypeDescriptor sqlTypeDescriptor) {
if ( sqlTypeDescriptor == null ) {
@ -344,9 +372,6 @@ public abstract class Dialect {
* @param sqlCode A {@link Types} constant indicating the SQL column type
* @return The {@link SqlTypeDescriptor} that should override the
* "standard basic" SQL type descriptor, or null, if there is no override.
*
* @see {@link SqlTypeDescriptor}
* @see {@link StandardBasicTypes#isStandardBasicSqlTypeDescriptor(org.hibernate.type.descriptor.sql.SqlTypeDescriptor)}
*/
protected SqlTypeDescriptor getSqlTypeDescriptorOverride(int sqlCode) {
SqlTypeDescriptor descriptor;

View File

@ -25,11 +25,10 @@ package org.hibernate.type;
import java.io.Serializable;
import org.hibernate.dialect.Dialect;
import org.hibernate.type.descriptor.java.BooleanTypeDescriptor;
import org.hibernate.type.descriptor.sql.BitTypeDescriptor;
import org.hibernate.type.descriptor.sql.SqlTypeDescriptor;
/**
* A type that maps between {@link java.sql.Types#BIT BIT} and {@link Boolean}
* A type that maps between {@link java.sql.Types#BOOLEAN BOOLEAN} and {@link Boolean}
*
* @author Gavin King
* @author Steve Ebersole
@ -40,7 +39,7 @@ public class BooleanType
public static final BooleanType INSTANCE = new BooleanType();
public BooleanType() {
this( BitTypeDescriptor.INSTANCE, BooleanTypeDescriptor.INSTANCE );
this( org.hibernate.type.descriptor.sql.BooleanTypeDescriptor.INSTANCE, BooleanTypeDescriptor.INSTANCE );
}
protected BooleanType(SqlTypeDescriptor sqlTypeDescriptor, BooleanTypeDescriptor javaTypeDescriptor) {

View File

@ -0,0 +1,70 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2011, Red Hat Inc. or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.type.descriptor.sql;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import org.hibernate.type.descriptor.ValueBinder;
import org.hibernate.type.descriptor.ValueExtractor;
import org.hibernate.type.descriptor.WrapperOptions;
import org.hibernate.type.descriptor.java.JavaTypeDescriptor;
/**
* Descriptor for {@link java.sql.Types#BOOLEAN BOOLEAN} handling.
*
* @author Steve Ebersole
*/
public class BooleanTypeDescriptor implements SqlTypeDescriptor {
public static final BooleanTypeDescriptor INSTANCE = new BooleanTypeDescriptor();
public int getSqlType() {
return Types.BOOLEAN;
}
@Override
public boolean canBeRemapped() {
return true;
}
public <X> ValueBinder<X> getBinder(final JavaTypeDescriptor<X> javaTypeDescriptor) {
return new BasicBinder<X>( javaTypeDescriptor, this ) {
@Override
protected void doBind(PreparedStatement st, X value, int index, WrapperOptions options) throws SQLException {
st.setBoolean( index, javaTypeDescriptor.unwrap( value, Boolean.class, options ) );
}
};
}
public <X> ValueExtractor<X> getExtractor(final JavaTypeDescriptor<X> javaTypeDescriptor) {
return new BasicExtractor<X>( javaTypeDescriptor, this ) {
@Override
protected X doExtract(ResultSet rs, String name, WrapperOptions options) throws SQLException {
return javaTypeDescriptor.wrap( rs.getBoolean( name ), options );
}
};
}
}