今天在写程序的时候碰到了Hibernate3.2.5: No Dialect mapping for JDBC type: -1
通过hql 语句没有问题,通过sql 就会有问题, 原因就是sql 在查找的字段中包含了ntext 这类的字段类型,报错了,hibernate 中 在sql 会报错。
环境:MySQL6.5+Hibernate3.2.5,使用JDK1.5
java代码大致这样的:
错误Exception:
import org.hibernate.dialect.SQLServerDialect; 再次运行程序,一切OK
from:http://blog.tremend.ro/2007/05/23/hibernate-utf-8-and-sql-server-2005/ I found out today that MS Sql server seems to handle Unicode in a very special way. Instead of having some support a database or table level, each Unicode column have to be created as “national”. That is be either nchar, nvarchar or ntext. Ms SQL Server 2005 seems to go one step further by announcing future deprecation for ntext
, text
and image
types. From Sql Server 2005 notes
: “ntext
, text
, and image
data types will be removed in a future version of Microsoft SQL Server. Avoid using these data types in new development work, and plan to modify applications that currently use them. Use nvarchar(max)
, varchar(max)
, and varbinary(max)
instead.” When working with Hibernate it seems there is no dialect to handle Unicode integration properly. You have to get down and write a custom dialect that maps to the new data types. From:http://wyhlgx.blog.163.com/blog/static/8072222520082189372145/
解决方法
:写一个类、修改hibernate配置文件。 写一个Dialect的子类,这里我 extends MySQL5Dialect类:
修改Hibernate配置文件hibernate.cfg.xml,把
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
修改为:
<property name="dialect">com.ibm.crl.inkfish.config.DialectForInkfish</property>
到了这一步就明朗了,原来是Hibernate的hibernate.cfg.xml中dialect没有注册ntext的类型。那好,自己注册一个吧。
2.定义MySQLServerDialect
package com.accp.birdbbs.orm;
import org.hibernate.Hibernate;
public class MySQLServerDialect extends SQLServerDialect
{
public MySQLServerDialect()
{
super();
registerHibernateType(Types.LONGVARCHAR, Hibernate.TEXT.getName());
}
}
3.在hibernate.cfg.xml中使用MySQLServerDialect
<property name="dialect">
com.accp.birdbbs.orm.MySQLServerDialect
</property>
May 23, 2007
Hibernate, UTF-8 and SQL Server 2005
/**
* Unicode support in SQL Server
*
* @author icocan
*/
public class UnicodeSQLServerDialect extends SQLServerDialect {
public UnicodeSQLServerDialect() {
super();
// Use Unicode Characters
registerColumnType(Types.VARCHAR, 255, "nvarchar($l)");
registerColumnType(Types.CHAR, "nchar(1)");
registerColumnType(Types.CLOB, "nvarchar(max)");
// Microsoft SQL Server 2000 supports bigint and bit
registerColumnType(Types.BIGINT, "bigint");
registerColumnType(Types.BIT, "bit");
}
}