org.hibernate.HibernateException: Connection cannot be null when 'hibernate.dialect' not set
来源:互联网 发布:bong3和小米手环2知乎 编辑:程序博客网 时间:2024/06/06 00:07
在Hibernate4中使用时出现的问题。
配置好Hibernate4后测试时出现Caused by: org.hibernate.HibernateException: Connection cannot be null when ‘hibernate.dialect’ not set
检查后hibernate.cfg.xml配置都是正确的,主要原因在于下面的代码:
Configuration cfg=new Configuration(); // cfg.buildSessionFactory();//在hibernate3中这么使用 ServiceRegistry registry=new ServiceRegistryBuilder().applySettings(cfg.getProperties()).buildServiceRegistry(); SessionFactory factory=cfg.buildSessionFactory(registry); Session session=factory.openSession();
注意第二行的applySettings(cfg.getProperties())是获取hibernate.cfg.xml的配置信息后设置进ServiceRegistryBuilder中,但是在设置之前的cfg(也就是Configuration)没有获取到hibernate.cfg.xml中的信息,所以在运行的时候或出现属性值未设置的情况。
知道了问题的原因所在,解决就好办了,只要在applySettings(cfg.getProperties())之前获取hibernate.cfg.xml的信息并设置到ServiceRegistryBuilder中即可,这样ServiceRegistryBuilder就可以通过这些信息来构建ServiceRegistry。可以通过Configuration的configure()方法获取的hibernate.cfg.xml中的配置信息。
Configuration cfg=new Configuration().configure(); // cfg.buildSessionFactory();//在hibernate3中这么使用 ServiceRegistry registry=new ServiceRegistryBuilder().applySettings(cfg.getProperties()).buildServiceRegistry(); SessionFactory factory=cfg.buildSessionFactory(registry); Session session=factory.openSession();
运行正常…
这是Hibernate4获取SessionFactory的方式,原来的buildSessionFactory()已经不推荐在Hibernate4中使用了。
0 0
- org.hibernate.HibernateException: Connection cannot be null when 'hibernate.dialect' not set 解决方法
- org.hibernate.HibernateException: Connection cannot be null when 'hibernate.dialect' not set
- org.hibernate.HibernateException: Connection cannot be null when 'hibernate.dialect' not set .
- org.hibernate.HibernateException: Connection cannot be null when 'hibernate.dialect' not set
- Caused by: org.hibernate.HibernateException: Connection cannot be null when 'hibernate.dialect' not
- Connection cannot be null when 'hibernate.dialect' not set
- Connection cannot be null when 'hibernate.dialect' not set
- Connection cannot be null when 'hibernate.dialect' not set
- Connection cannot be null when 'hibernate.dialect' not set
- hibernate第二篇 org.hibernate.HibernateException: Connection cannot be null when 'hibernate.dialect' no
- Hibernate4后出现: org.hibernate.HibernateException: Connection cannot be null when 'hibernate.dialect'
- Hibernate Connection cannot be null when 'hibernate.dialect' not set错误
- 正确决解Hibernate4.*中:Connection cannot be null when 'hibernate.dialect' not set
- HIbernate4.2.7错误Connection cannot be null when 'hibernate.dialect' not set
- Connection cannot be null when 'hibernate.dialect' not set问题的解决
- 异常:org.hibernate.HibernateException: 'hibernate.dialect' must be set when no Connection available
- ssh整合Caused by: org.hibernate.HibernateException: 'hibernate.dialect' must be set when no Connection
- org.hibernate.HibernateException: 'hibernate.dialect' must be set when no Connection available
- SDK安装时general errors & sols
- CentOS/Ubuntu 14.10 安装 phpstorm
- 软件调试实战入门(1)
- 65_Valid Number
- Linux下vim的配置文件
- org.hibernate.HibernateException: Connection cannot be null when 'hibernate.dialect' not set
- FTP显示illegal PORT command 、Unknown Command解决方案
- JSplitPane的使用
- Android中将布局文件/View添加至窗口过程分析 ---- 从setContentView()谈起
- AsyncQueryHandler详解
- LaTeX新人教程,30分钟从完全陌生到基本入门
- 莫名其妙的 do{ } while(0)
- windows下安装beautifulsoup4.3
- 第四周上机项目2 分数类的雏形