Hibernate3.2.5: No Dialect mapping for JDBC type: -1

今天在写程序的时候碰到了Hibernate3.2.5: No Dialect mapping for JDBC type: -1

通过hql 语句没有问题,通过sql 就会有问题, 原因就是sql 在查找的字段中包含了ntext 这类的字段类型,报错了,hibernate 中 在sql 会报错。

环境:MySQL6.5+Hibernate3.2.5,使用JDK1.5 


java代码大致这样的: 
Session sess = MysqlFactory.openSession();    //MysqlFactory是我自己写得一个类 
List list = new ArrayList(); 
try{ 
    list = sess.createSQLQuery ("select * from table_1").list(); 
    sess.flush(); 
}catch(Exception ex){ 
    ex.printStackTrace(); 
}finally{ 
    MysqlFactory.closeSession(); 


错误Exception: 
org.hibernate.MappingException: No Dialect mapping for JDBC type: -1 
    at org.hibernate.dialect.TypeNames.get(TypeNames.java:56) 
    at org.hibernate.dialect.TypeNames.get(TypeNames.java:81) 

问题原因:数据库表中有text类型的字段,而Hibernate在native查询中没有注册这个字段,因此发生这个错误。 

解决方法 :写一个类、修改hibernate配置文件。 写一个Dialect的子类,这里我 extends MySQL5Dialect类: 
package xxx.xxx;    //xxx.xxx自己根据情况来写  import java.sql.Types; 
import org.hibernate.dialect.MySQL5Dialect; 
public class DialectForInkfish extends MySQL5Dialect { 
    public DialectForInkfish() { 
        super(); 
        registerHibernateType(Types.LONGVARCHAR, 65535, "text"); 
    } 

修改Hibernate配置文件hibernate.cfg.xml,把
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property> 
修改为:
<property name="dialect">com.ibm.crl.inkfish.config.DialectForInkfish</property>
 
hibernate ntext 问题:
1.先用FrontEnd Plus反编译SQLServerDialect,发现SQLServerDialect继承SybaseDialect,继承 Dialect,Dialect中有registerHibernateType方法,再查看SybaseDialect构造中 registerColumnType没有注册ntext类型。
到了这一步就明朗了,原来是Hibernate的hibernate.cfg.xml中dialect没有注册ntext的类型。那好,自己注册一个吧。
2.定义MySQLServerDialect
package com.accp.birdbbs.orm;

import org.hibernate.dialect.SQLServerDialect;
import java .sql.Types;
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>

再次运行程序,一切OK

 


from:http://blog.tremend.ro/2007/05/23/hibernate-utf-8-and-sql-server-2005/

May 23, 2007

Hibernate, UTF-8 and SQL Server 2005

Filed under: Java, General — Ioan Cocan @ 12:54 pm

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.

 

/**
 * 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");
    }
}

From:http://wyhlgx.blog.163.com/blog/static/8072222520082189372145/

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

«    2024年11月    »
123
45678910
11121314151617
18192021222324
252627282930
搜索
标签列表
网站分类
最新留言
    文章归档
    友情链接

    Powered By Z-BlogPHP 1.7.3

    Copyright Your WebSite.Some Rights Reserved.闽ICP备11018667号-2