对于Hibernbate SQLServer 2008 No Dialect mapping for JDBC type:-9解决方法
Posted KdanMin
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了对于Hibernbate SQLServer 2008 No Dialect mapping for JDBC type:-9解决方法相关的知识,希望对你有一定的参考价值。
今天同事遇到一个hibernate.mapping.exception,然后帮忙解决啦!下面整理下解决方法
异常重现
第一种通过整理hibernate.cfg.xml Dialect
package com.util;
import java.sql.Types;
import org.hibernate.Hibernate;
import org.hibernate.dialect.SQLServerDialect;
public class MyDialect extends SQLServerDialect
public MyDialect()
super();
//对应hbibernate里面的数据类型,否则映射不正确!
registerHibernateType(Types.NVARCHAR, Hibernate.STRING.getName());
registerHibernateType(Types.LONGVARCHAR, Hibernate.TEXT.getName());
//registerHibernateType(Types.DECIMAL, Hibernate.BIG_DECIMAL.getName());
// registerHibernateType(Types.LONGVARCHAR, 65535, "text");//.LONGVARCHAR
参考http://blog.csdn.net/qq804702802/article/details/47754875
然后在hibernate.cfg.xml配置不要使用原生的hibernate查询 SQL
第二种创建具体代码如下UserInfoImpl.java
@Override
public List getListBySql()
// TODO Auto-generated method stub
List list = new ArrayList();
Transaction tr = null;
Session session = null;
try
session = getSession();
tr = session.beginTransaction();
String hql = "select * from UserInfo";
//创建sql查询
SQLQuery query = session.createSQLQuery(hql);
//指定UserInfo.hbm.xmlColumn 以及指定Hibernate数据类型一直
query.addScalar("U_Name", Hibernate.STRING);
query.addScalar("U_Pwd", Hibernate.STRING);
list = query.list();
tr.commit();
System.out.println(hql);
catch (HibernateException e)
// TODO Auto-generated catch block
e.printStackTrace();
tr.rollback();
return list;
@Override
public List<UserInfo> getListBySqlQ()
// TODO Auto-generated method stub
// TODO Auto-generated method stub
List<UserInfo> list = new ArrayList<UserInfo>();
Transaction tr = null;
Session session = null;
try
session = getSession();
tr = session.beginTransaction();
String hql = "select * from UserInfo";//添加一个实体
list = session.createSQLQuery(hql).addEntity(UserInfo.class).list();
// list=query.list();
tr.commit();
System.out.println(hql);
catch (HibernateException e)
// TODO Auto-generated catch block
e.printStackTrace();
tr.rollback();
return list;
最后测试
package com.util;
import java.util.List;
import com.bean.LogInfo;
import com.bean.UserInfo;
import com.dao.UserDao;
import com.dao.impl.UserInfoImpl;
public class TestUser
public static void main(String[] args)
UserDao dao = new UserInfoImpl();
//返回object数组
List<Object[]> list = dao.getListBySql();
//逻辑与 先判断不为空 否则报NullPonitException
if (list != null && list.size() > 0)
for (Object[] objects : list)
//打印所有UserInfo的U_Name
System.out.println(objects[0]);
效果图
以上是关于对于Hibernbate SQLServer 2008 No Dialect mapping for JDBC type:-9解决方法的主要内容,如果未能解决你的问题,请参考以下文章