`

No Dialect mapping for JDBC type: -1;

阅读更多

系统报错:

No Dialect mapping for JDBC type: -1; nested exception is org.hibernate.MappingException: No Dialect mapping for JDBC type: -1

 

好几次碰到这个问题,每次都匆匆忙忙的解决了,这个写个东西记录下来,以后直接用。

 

一看见Dialect就知道这个Hibernate的方言出问题了,这个-1不在它原有的范围之内,需要手动加上去。

 

1、新建一个Class文件,里面添加上报错的类型,如:

package com.wjl.sqldialect;

import java.sql.Types;

import org.hibernate.Hibernate;
import org.hibernate.dialect.SQLServerDialect;

public class SqlServer2008Dialect extends SQLServerDialect {
	public SqlServer2008Dialect() {
		super();
		//JDBC type:1
		registerHibernateType(Types.CHAR, Hibernate.STRING.getName());
		
		//JDBC type:-9
		registerHibernateType(Types.NVARCHAR, Hibernate.STRING.getName());
		
		//JDBC type:-16
		registerHibernateType(Types.LONGNVARCHAR, Hibernate.STRING.getName());
		
		//JDBC type:3
		registerHibernateType(Types.DECIMAL, Hibernate.DOUBLE.getName());
		
		//JDBC type:-1
		registerHibernateType(Types.LONGVARCHAR, Hibernate.STRING.getName());  
	}
}

 

 2、将配置文件中的Dialect配置上面写的那个,即:

<property name="hibernateProperties">
	<props>
		<prop key="hibernate.dialect">
				org.hibernate.dialect.SQLServerDialect
		</prop>
		<prop key="hibernate.show_sql">true</prop>
	</props>
</property>

//替换成
<property name="hibernateProperties">
	<props>
		<prop key="hibernate.dialect">
				com.wjl.sqldialect.SqlServer2008Dialect
		</prop>
		<prop key="hibernate.show_sql">true</prop>
	</props>
</property>

 至于上面那些数据,-1,-9,-16,3什么的,它们都是java.sql.Types维护的一些常量,主要有以下这些(摘自JDK1.6帮助文档):

public static final int ARRAY 2003
public static final int BIGINT -5
public static final int BINARY -2
public static final int BIT -7
public static final int BLOB 2004
public static final int BOOLEAN 16
public static final int CHAR 1
public static final int CLOB 2005
public static final int DATALINK 70
public static final int DATE 91
public static final int DECIMAL 3
public static final int DISTINCT 2001
public static final int DOUBLE 8
public static final int FLOAT 6
public static final int INTEGER 4
public static final int JAVA_OBJECT 2000
public static final int LONGNVARCHAR -16
public static final int LONGVARBINARY -4
public static final int LONGVARCHAR -1
public static final int NCHAR -15
public static final int NCLOB 2011
public static final int NULL 0
public static final int NUMERIC 2
public static final int NVARCHAR -9
public static final int OTHER 1111
public static final int REAL 7
public static final int REF 2006
public static final int ROWID -8
public static final int SMALLINT 5
public static final int SQLXML 2009
public static final int STRUCT 2002
public static final int TIME 92
public static final int TIMESTAMP 93
public static final int TINYINT -6
public static final int VARBINARY -3
public static final int VARCHAR 12
所以每次报错说是哪个数字时,直接到JDK帮助文档上找到这些常量,找到对应的直接添加到SqlServer2008Dialect类中就成。
譬如报错-1,对应于Types中的LONGVARCHAR,就在SqlServer2008Dialect添加一句:
registerHibernateType(Types.LONGVARCHAR, Hibernate.STRING.getName());

要是报错的数字没有包含在Types中(可能性不大),就直接写上数字,告诉Hibernate用String来处理这种类型,也就是:

registerHibernateType(@报错数字, Hibernate.STRING.getName());

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics