MySQL数据库中有张表的字段是text,查询出来后对应的java类型是String,Dialect设置为org.hibernate.dialect.MySQLDialect
运行的时候报错:No Dialect mapping for JDBC type:-1
错误的原因是MySQL和java类型转换错误,解决方法如下:
1.首先自己写一个class,继承org.hibernate.dialect.MySQLDialect
package com.util;
import java.sql.Types;
import org.hibernate.Hibernate;
import org.hibernate.dialect.MySQLDialect;
/**
*
* 解决No Dialect mapping for JDBC type: -1
* @author 邵海雄
*
*/
public class BlobMySQLDialect extends MySQLDialect {
public BlobMySQLDialect() {
super();
registerHibernateType(Types.LONGNVARCHAR,
Hibernate.TEXT.getName());
registerHibernateType(Types.LONGVARCHAR, Hibernate.TEXT.getName());
/////////mysql text字段
registerHibernateType(Types.DECIMAL,
Hibernate.BIG_DECIMAL.getName());
}
}
2.Dialect设置为:包名.BlobMySQLDialect
注:其他的数据类型请参考下表
类型名称 |
显示长度 |
数据库类型 |
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 |
//原文地址:http://blog.****.net/dr_lf/article/details/8225024
//相关问题:http://blog.****.net/xd195666916/article/details/5419316