org.hibernate.MappingException: No Dialect mapping for JDBC type: -9
原因:Hibernate框架的方言(Dialect )没有数据库中数据类型nvarchar的映射,只有varchar类型的映射。所以在启动时出现此异常。
解决方法:
方法一:
将数据库字段的nvarchar类型改为varchar类型。
方法二:
为了指定更加明确的返回值类型,我们可以使用addScalar()方法:
session.createSQLQuery("select * from person_inf")
.addScalar("name",StandardBasicTypes.STRING)
.addScalar("age",StandardBasicTypes.INT)
.list();
方法三:
org.hibernate.MappingException: No Dialect mapping for JDBC type: -9
org.hibernate.MappingException: No Dialect mapping for JDBC type: -15
笔者在做连接SQLServer2008数据库的时候遇到了上述的错误,解决办法如下:
package com.shihuan.core.framework.extend;
import java.sql.Types;
import org.hibernate.Hibernate;
import org.hibernate.dialect.SQLServer2008Dialect;
public class SqlServer2008Dialect extends SQLServer2008Dialect {
public SqlServer2008Dialect() {
super();
registerHibernateType(Types.CHAR, Hibernate.STRING.getName());
registerHibernateType(Types.NCHAR, Hibernate.STRING.getName());
registerHibernateType(Types.NVARCHAR, Hibernate.STRING.getName());
registerHibernateType(Types.LONGNVARCHAR, Hibernate.STRING.getName());
registerHibernateType(Types.DECIMAL, Hibernate.DOUBLE.getName());
}
}
java.sql.Types对应的值请参考http://blog.sina.com.cn/s/blog_4f925fc30102ep0v.html
java.sql.Types对应的sql server2008中的一些值:
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;
Hibernate SQL方言(hibernate.dialect):
RDBMS | Dialect |
DB2 | org.hibernate.dialect.DB2Dialect |
DB2 AS/400 | org.hibernate.dialect.DB2400Dialect |
DB2 OS390 | org.hibernate.dialect.DB2390Dialect |
PostgreSQL | org.hibernate.dialect.PostgreSQLDialect |
MySQL5 | org.hibernate.dialect.MySQL5Dialect |
MySQL5 with InnoDB | org.hibernate.dialect.MySQL5InnoDBDialect |
MySQL with MyISAM | org.hibernate.dialect.MySQLMyISAMDialect |
Oracle(any version) | org.hibernate.dialect.OracleDialect |
Oracle 9i | org.hibernate.dialect.Oracle9iDialect |
Oracle 10g | org.hibernate.dialect.Oracle10gDialect |
Oracle 11g | org.hibernate.dialect.Oracle10gDialect |
Sybase | org.hibernate.dialect.SybaseASE15Dialect |
Sybase Anywhere | org.hibernate.dialect.SybaseAnywhereDialect |
Microsoft SQL Server 2000 | org.hibernate.dialect.SQLServerDialect |
Microsoft SQL Server 2005 | org.hibernate.dialect.SQLServer2005Dialect |
Microsoft SQL Server 2008 | org.hibernate.dialect.SQLServer2008Dialect |
SAP DB | org.hibernate.dialect.SAPDBDialect |
Informix | org.hibernate.dialect.InformixDialect |
HypersonicSQL | org.hibernate.dialect.HSQLDialect |
H2 Database | org.hibernate.dialect.H2Dialect |
Ingres | org.hibernate.dialect.IngresDialect |
Progress | org.hibernate.dialect.ProgressDialect |
Mckoi SQL | org.hibernate.dialect.MckoiDialect |
Interbase | org.hibernate.dialect.InterbaseDialect |
Pointbase | org.hibernate.dialect.PointbaseDialect |
FrontBase | org.hibernate.dialect.FrontbaseDialect |
Firebird | org.hibernate.dialect.FirebirdDialect |