hibernate:MySQL No Dialect mapping for JDBC type: -1

出處:(hibernate中使用原生的sql語句,報以下錯誤:)java

  MySQL No Dialect mapping for JDBC type: -1

代碼:  mysql

List list = session.createSQLQuery(sql).list();

分析:產生些問題的緣由是因表中的某個字段類型在hibernate中找不到對應的類型。而致使的,每一種類型都對應一種type:sql

類型名稱 顯示長度 數據庫類型 JAVA類型 JDBC類型(int) Types屬性
VARCHAR L+N VARCHAR java.lang.String 12  Types.VARCHAR
CHAR N CHAR java.lang.String 1  Types.CHAR
BLOB L+N BLOB java.lang.byte[] -4  Types.LONGVARBINARY
TEXT 65535 VARCHAR java.lang.String -1  Types.LONGVARCHAR
INTEGER 4 INTEGER UNSIGNED java.lang.Long 4  Types.INTEGER
TINYINT 3 TINYINT UNSIGNED java.lang.Integer -6  Types.TINYINT
SMALLINT 5 SMALLINT UNSIGNED java.lang.Integer 5  Types.SMALLINT
MEDIUMINT 8 MEDIUMINT UNSIGNED java.lang.Integer 4  Types.INTEGER
BIT 1 BIT java.lang.Boolean -7  Types.BIT
BIGINT 20 BIGINT UNSIGNED java.math.BigInteger -5  Types.BIGINT
FLOAT 4+8 FLOAT java.lang.Float 7  Types.REAL
DOUBLE 22 DOUBLE java.lang.Double 8  Types.DOUBLE
DECIMAL 11 DECIMAL java.math.BigDecimal 3  Types.DECIMAL
BOOLEAN 1 同TINYINT java.lang.Integer -6  Types.TINYINT
DATE 10 DATE java.sql.Date 91  Types.DATE
TIME 8 TIME java.sql.Time 92  Types.TIME
DATETIME 19 DATETIME java.sql.Timestamp 93  Types.TIMESTAMP
TIMESTAMP 19 TIMESTAMP java.sql.Timestamp 93  Types.TIMESTAMP
YEAR 4 YEAR java.sql.Date 91  Types.DATE

解決方法:1數據庫

更新hibernate方言(由於最新的hibernate言可能提供了對這種類型的支持)如下是言的彙總:session

RDBMS 方言
DB2 org.hibernate.dialect.DB2Dialect
DB2 AS/400 org.hibernate.dialect.DB2400Dialect
DB2 OS390 org.hibernate.dialect.DB2390Dialect
PostgreSQL org.hibernate.dialect.PostgreSQLDialect
MySQL org.hibernate.dialect.MySQLDialect
MySQL with InnoDB org.hibernate.dialect.MySQLInnoDBDialect
MySQL with MyISAM org.hibernate.dialect.MySQLMyISAMDialect
Oracle (any version) org.hibernate.dialect.OracleDialect
Oracle 9i/10g org.hibernate.dialect.Oracle9Dialect
Sybase org.hibernate.dialect.SybaseDialect
Sybase Anywhere org.hibernate.dialect.SybaseAnywhereDialect
Microsoft SQL Server org.hibernate.dialect.SQLServerDialect
SAP DB org.hibernate.dialect.SAPDBDialect
Informix org.hibernate.dialect.InformixDialect
HypersonicSQL org.hibernate.dialect.HSQLDialect
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

以mysql爲例:若是之前org.hibernate.dialect.MySQLDialect,那麼如今改爲:org.hibernate.dialect.MySQLInnoDBDialectapp

 

方法2: 若是更新方言仍是不行,那能夠本身寫方言,並配置到hibernate中..spa

package xm.helper;

import java.sql.Types;
import org.hibernate.Hibernate;
import org.hibernate.dialect.MySQLDialect;
import org.hibernate.dialect.function.StandardSQLFunction;

public class XmMySQLDialect extends MySQLDialect {


public XmMySQLDialect (){
        //把報錯的類型註冊到hibernate類型的映射中去....
        registerHibernateType(-1, Hibernate.STRING.getName());
        //也能夠把mysql類型註冊到,好比hibernate支持longtext
        registerColumnType(Types.VARCHAR, "longtext");
    }
}

修改hibernate的配置文件:hibernate

<property name="hibernate.dialect">xm.helper.XmMySQLDialect</property>

 

方法3:code

使用原生的jdbc,本身包裝,不使用createSQLQuery()方法;orm

public List<Object[]> exeQuery(final String sql) {
        final List<Object[]> results = new ArrayList<Object[]>();
        sessionFactory.openSession().doWork(new Work() {
            
            public void execute(Connection paramConnection) throws SQLException {
                java.sql.PreparedStatement psta = paramConnection.prepareStatement(sql);
                ResultSet rs = psta.executeQuery();
                int columnCount = rs.getMetaData().getColumnCount();
                
                while(rs.next()){
                    Object[] rows = new Object[columnCount];
                       for (int j=0; j<columnCount; ++j) {
                           rows[j] = rs.getObject(j+1);
                        }
                       results.add(rows);
                    }
          rs.close();      
                psta.close();
                paramConnection.close(); } });
return results; }
相關文章
相關標籤/搜索