为了实现在用户误操作后能够对数据进行修正,打算添加一个数据库管理功能,主要实现对数据表的查询和执行sql语句。费了半天功夫完成以后查询有的表数据正常显示,但是个别表出现如下错误:
No Dialect mapping for JDBC type: -1
意思显而易见:没有方言能够与JDBC类型-1映射。难道我数据库字段用了特殊类型,而hibernate不支持?百度之后找到很多同样错误,有的分析发现对于长度过大的varchar(LONGVARCHAR )映射时会报这个错误,检查数据库发现这些表中都是用了text类型,应该就是这个原因。修改方法一般有两种:一是修改字段类型,二是扩展自定义方言。采用第二种方法修改如下:
添加自定义方言类:
package com.sxpt.util; import java.sql.Types; import org.hibernate.Hibernate; import org.hibernate.dialect.MySQL5Dialect; public class KtMySQL5Dialect extends MySQL5Dialect{ public KtMySQL5Dialect(){ super(); this.registerHibernateType(Types.LONGVARCHAR, Hibernate.STRING.getName()); } }
hibernate.xml也要修改为对应方言:
<property name="hibernate.dialect"> com.sxpt.util.KtMySQL5Dialect </property>
下附java.sql.Types对应值,在出现错误时可根据值判断类型
public final static int BIT = -7; public final static int TINYINT = -6; public final static int SMALLINT = 5; public final static int INTEGER = 4; public final static int BIGINT = -5; public final static int FLOAT = 6; public final static int REAL = 7; public final static int DOUBLE = 8; public final static int NUMERIC = 2; public final static int DECIMAL = 3; public final static int CHAR = 1; public final static int VARCHAR = 12; public final static int LONGVARCHAR = -1; public final static int DATE = 91; public final static int TIME = 92; public final static int TIMESTAMP = 93; public final static int BINARY = -2; public final static int VARBINARY = -3; public final static int LONGVARBINARY = -4; public final static int NULL = 0; public final static int OTHER = 1111; public final static int JAVA_OBJECT = 2000; public final static int DISTINCT = 2001; public final static int STRUCT = 2002; public final static int ARRAY = 2003; public final static int BLOB = 2004; public final static int CLOB = 2005; public final static int REF = 2006; public final static int DATALINK = 70; public final static int BOOLEAN = 16; public final static int ROWID = -8; public static final int NCHAR = -15; public static final int NVARCHAR = -9; public static final int LONGNVARCHAR = -16; public static final int NCLOB = 2011; public static final int SQLXML = 2009;