Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set

错误信息:

Exception in thread "main" org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set

    at org.hibernate.engine.jdbc.dialect.internal.DialectFactoryImpl.determineDialect(DialectFactoryImpl.java:104)
    at org.hibernate.engine.jdbc.dialect.internal.DialectFactoryImpl.buildDialect(DialectFactoryImpl.java:71)
    at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:209)
    at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:111)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:234)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:206)
    at org.hibernate.cfg.Configuration.buildTypeRegistrations(Configuration.java:1887)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1845)
    at com.yangwan.test.MainTest.createSessionV3(MainTest.java:77)

    at com.yangwan.test.MainTest.main(MainTest.java:102)

右上方信息可以定位到出错代码:

Configuration configuration = new Configuration().configure();
		StandardServiceRegistryBuilder ssrb = new StandardServiceRegistryBuilder();
		ServiceRegistry sr = ssrb.build();
		SessionFactory sessionFactory = configuration.buildSessionFactory(sr);
		Session session = sessionFactory.openSession();
		Transaction transaction = session.beginTransaction();
		Person temp = new Person("xiaoxiao",22);
		try{
			session.save(temp);
			transaction.commit();
		}catch(Exception ex){
			ex.printStackTrace();
			transaction.rollback();
		}finally{
			session.close();
		}

SessionFactory sessionFactory = configuration.buildSessionFactory(sr);--->出现错误。


解决方法:

       Hibernate v4以后就已经废弃了利用ServiceRegister来创建SessionFactory了。所有创建SessionFactory的方法在Hibernate v4后就不适合了。

正确的SessionFactory创建代码:

Configuration cfg = new Configuration().configure();
StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(cfg.getProperties()).build();
SessionFactory sessionFactory = cfg.buildSessionFactory(serviceRegistry);


你可能感兴趣的:(开发环境配置)