Connection cannot be null when 'hibernate.dialect' not set问题的解决
来源:互联网 发布:网络拓扑算法 编辑:程序博客网 时间:2024/05/22 18:55
本人刚开始学hibernate的小白,今天遇到了这样的问题,下面贴出解决方法,希望对其他遇到同样问题的初学者有帮助
import org.hibernate.HibernateException;
import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.cfg.Configuration;import org.hibernate.service.ServiceRegistry;import org.hibernate.service.ServiceRegistryBuilder;public class HibernateUtil {private static final ThreadLocal<Session> threadLocal=new ThreadLocal<Session>();private static SessionFactory sessionFactory=null;static {try{/*Configuration cfg = new Configuration().configure("hibernate.cfg.xml");sessionFactory = cfg.buildSessionFactory();*/Configuration cfg = new Configuration();ServiceRegistry serviceRegistry =new ServiceRegistryBuilder().applySettings(cfg.getProperties()).buildServiceRegistry();sessionFactory = cfg.buildSessionFactory(serviceRegistry);}catch(HibernateException e){System.out.println("创建会话工厂失败");e.printStackTrace();}}public static Session getsession() throws HibernateException{Session session=(Session)threadLocal.get();if(session==null||!session.isOpen()){if(sessionFactory==null){rebuildsessionFactory();}session=(sessionFactory!=null)?sessionFactory.openSession():null;threadLocal.set(session);}return session;}public static void rebuildsessionFactory(){try{/*Configuration cfg = new Configuration().configure();sessionFactory = cfg.buildSessionFactory();*/Configuration cfg = new Configuration().configure();ServiceRegistry serviceRegistry =new ServiceRegistryBuilder().applySettings(cfg.getProperties()).buildServiceRegistry();sessionFactory = cfg.buildSessionFactory(serviceRegistry);}catch(Exception e){System.out.println("创建会话工厂失败");e.printStackTrace();}}public static SessionFactory getsessionFactory(){return sessionFactory;}public static void closesession() throws HibernateException{Session session=(Session)threadLocal.get();threadLocal.set(null);if(session!=null){session.close();}}public static void main(String[] args) { System.out.println(getsessionFactory()); }}
先贴上我的代码,
运行后控制台输出如下:
十一月 15, 2016 5:27:18 下午 org.hibernate.annotations.common.Version <clinit>INFO: HCANN000001: Hibernate Commons Annotations {4.0.2.Final}十一月 15, 2016 5:27:19 下午 org.hibernate.Version logVersionINFO: HHH000412: Hibernate Core {4.2.4.Final}十一月 15, 2016 5:27:19 下午 org.hibernate.cfg.Environment <clinit>INFO: HHH000206: hibernate.properties not found十一月 15, 2016 5:27:19 下午 org.hibernate.cfg.Environment buildBytecodeProviderINFO: HHH000021: Bytecode provider name : javassist十一月 15, 2016 5:27:19 下午 org.hibernate.service.jdbc.connections.internal.ConnectionProviderInitiator initiateServiceWARN: HHH000181: No appropriate connection provider encountered, assuming application will be supplying connections创建会话工厂失败org.hibernate.HibernateException: Connection cannot be null when 'hibernate.dialect' not setat org.hibernate.service.jdbc.dialect.internal.DialectFactoryImpl.determineDialect(DialectFactoryImpl.java:97)at org.hibernate.service.jdbc.dialect.internal.DialectFactoryImpl.buildDialect(DialectFactoryImpl.java:67)at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:174)at org.hibernate.service.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:75)at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:159)at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:131)at org.hibernate.cfg.Configuration.buildTypeRegistrations(Configuration.java:1818)at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1776)at hibernate.HibernateUtil.<clinit>(HibernateUtil.java:19)null
这是因为下面这行代码后面缺少.configure(),没有导入配置文件导致的
Configuration cfg = new Configuration();
这行代码改为:
Configuration cfg = new Configuration().configure();根据错误提示Connection cannot be null when 'hibernate.dialect' not set猜想,可能只要是没找到hibernate.dialect都会输出同样的错误,会有其他的问题导致同样的错误。有遇到人的可以在下面补充下。0 0
- 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
- Connection cannot be null when 'hibernate.dialect' not set
- Hibernate 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 .
- org.hibernate.HibernateException: Connection cannot be null when 'hibernate.dialect' not set
- 马士兵的第一个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
- 异常记录001—hibernate 4.2.4 Final 测试出现Connection cannot be null when 'hibernate.dialect' not set
- Caused by: org.hibernate.HibernateException: Connection cannot be null when 'hibernate.dialect' not
- [Hibernate]Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set
- Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set——【hibernate 日常错误】
- 创建SessionFactory出错Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set
- java 语言简介
- 用sublime sever启动本地服务(手机访问电脑页面)
- 给div设置blur事件
- java下载文件名称各种浏览器兼容问题
- 【HDU】-1568-Fibonacci(公式+log取小数)
- Connection cannot be null when 'hibernate.dialect' not set问题的解决
- Trafodion TMUDF(Table-Mapping UDF)
- Android-设置屏幕亮度默认为自动调节
- VS2013 MFC基于对话框编程
- Mybatis极其(最)简(好)单(用)的一个分页插件
- Objective-C Runtime[细读]
- c语言中对文件修改后放入同一文件中
- mysql导入数据load data infile用法(将txt文件中的数据导入表中)
- Structs2 输入校验,Action中校验,在xml中校验