日期:2014-05-16  浏览次数:20417 次

No Dialect mapping for JDBC type: 7
网上搜的

No Dialect mapping for JDBC type: 7
I got this error while I was working with hibernate and SQL Server. Following are the findings I came across. Hope this helps you too rectify this error.

Following questions are relevant to understand this clearly.

What is jdbc type 7?
java.sql.Type.REAL : It is a constant in the java programming language, that identifies SQL type REAL.
Value of this constant is 7. This is the above mentioned jdbc type 7.

What is dialect?
Dialect is the type of the database that hibernate is going to use. Following are the commonly used dialects. These are the subclasses of the org.hibernate.dialect.Dialect for specific databases.

org.hibernate.dialect.HSQLDialect
org.hibernate.dialect.Oracle9Dialect
org.hibernate.dialect.MySQLDialect
org.hibernate.dialect.SQLServerDialect
org.hibernate.dialect.FirebirdDialect
What cause this error to occur?
The database may contain a table with field of datatype real. Hibernate dialect SQLServerDialect doesnot understand this type. So we need to explicitly convert this real type to one that dialect can understand. One way to achieve this is to write a subclass of org.hibernate.dialect.SQLServerDialect.


package co.nr.javaalert.hibernate.dialect;
import java.sql.Types;
public class SubSQLServerDialect extends org.hibernate.dialect.SQLServerDialect{
public SQLServerDialectForBilling() {
super();
registerColumnType(Types.REAL,"number($p,$s)");
registerHibernateType(Types.REAL,"double");
}
}

registerColumnType() method register a type name for a given type code. This step register sql column data type NUMBER(precision,scale) to Types.REAL. Then register this Types.REAL with a data type that can understand hibernate. The recommended Java mapping for the sql REAL type is as a Java float.

Use this subclass instead of org.hibernate.dialect.SQLServerDialect in hibernate.cfg.xml.

Reference
SQL dialects reference/Data structure definition/Data types/Numeric types