No Dialect mapping for JDBC type

xiaoxiao2026-03-18  4

这个问题很多人都遇到过,网上有很多文章可以搜得到 解决方法就是自定义一个Hibernate Dialect.

package com.yourcompany.util ; import java.sql.Types; import org.hibernate.Hibernate; import org.hibernate.dialect.MySQL5Dialect; public class CustomDialect extends MySQL5Dialect { public CustomDialect() { super(); registerHibernateType(Types.DECIMAL, Hibernate.BIG_DECIMAL.getName()); registerHibernateType(-1, Hibernate.STRING.getName()); } }

然后将hibernate配置文件(或是Spring配置文件)中配置数据库方言的那一项,改成上面自定义的方言.:

<property name="hibernate.dialect"> com.yourcompany.CustomDialect </property>

说明: 如果你的数据库是mysql,而又用了decimal类型,报错应该是 No Dialect mapping for JDBC type: 3 . 注意这个3, 它说明hibernate不能将这种数据类型映射到你的java类中. 就需要在自定义的方言中用到:

registerHibernateType(Types.DECIMAL, Hibernate.BIG_DECIMAL.getName());

这句代码. 如果你用了text数据类型,hibernate根本就不认识这种数据类型,所以会返回No Dialect mapping for JDBC type: -1

. 这样的话,就需要在方言中加入:

registerHibernateType(-1, Hibernate.STRING.getName());

 

于是, 万事大吉一切OK ^.^

转载请注明原文地址: https://www.6miu.com/read-5046139.html

最新回复(0)