官方文档提供的HibernateUtil是这样的 package org.hibernate.tutorial.util; import org.hibernate.SessionFactory; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.cfg.Configuration; public class HibernateUtil { private static final SessionFactory sessionFactory = buildSessionFactory(); private static SessionFactory buildSessionFactory() { try { // Create the SessionFactory from hibernate.cfg.xml //明显有语法错误,我在下面加了一句return new Configuration().configure().buildSessionFactory( new StandardServiceRegistryBuilder().build() ); } catch (Throwable ex) { // Make sure you log the exception, as it might be swallowed System.err.println("Initial SessionFactory creation failed." + ex); throw new ExceptionInInitializerError(ex); } } public static SessionFactory getSessionFactory() { return sessionFactory; } } 用这个报Access to DialectResolutionInfo cannot be null when “”hibernate.dialect”” not这个错误,然后在网上找到的解决方案改成下面这样,就没问题了 package com.example.util; import org.hibernate.SessionFactory; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.cfg.Configuration; import org.hibernate.service.ServiceRegistry; import org.hibernate.service.ServiceRegistryBuilder; public class HibernateUtil { private static final SessionFactory sessionFactory = buildSessionFactory(); private static SessionFactory buildSessionFactory() { try { // Create the SessionFactory from hibernate.cfg.xml Configuration cfg = new Configuration().configure(); ServiceRegistry serviceRegistry= new ServiceRegistryBuilder().applySettings(cfg.getProperties()).buildServiceRegistry(); return cfg.buildSessionFactory(serviceRegistry); // return new Configuration().configure().buildSessionFactory( // new StandardServiceRegistryBuilder().build() ); } catch (Throwable ex) { // Make sure you log the exception, as it might be swallowed System.err.println("Initial SessionFactory creation failed." + ex); throw new ExceptionInInitializerError(ex); } } public static SessionFactory getSessionFactory() { return sessionFactory; } } 到底是信官方的还是…..还有后面那个方法报了好几个deprecated的警告 |
|
5分 |
我也是刚学完hibernate 这是我用的hibernateUtil 没有报什么错
不过确实看不太懂。。会用就像 import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; public class HibernateUtil { private static final SessionFactory sessionFactory; static { try { // Create the SessionFactory from hibernate.cfg.xml sessionFactory = new Configuration().configure().buildSessionFactory(); } catch (Throwable ex) { // Make sure you log the exception, as it might be swallowed System.err.println("Initial SessionFactory creation failed." + ex); throw new ExceptionInInitializerError(ex); } } public static SessionFactory getSessionFactory() { return sessionFactory; } } |
[b][b][/b][/b]
恩 这个好像也有方法是deprecated,先学再说吧。。 |
|
还请大家给初学者一些建议和经验啊。。。
|
|
15分 |
你用的是哪个版本呀,如果是4的话用后者,用前者会报错好像是一个bug,如果是3的话那么用前者即可
|
5分 |
hibernate 3和4版本是不同的,前一个是3的,后一个是4的写法吧
|
5分 |
楼主改的并没有错,buildSessionFactory需要返回一个值但官方的没给,改成你那样是对的,kang2618在静态代码块里给sessionFactory赋值也是可以的
|
嗯,用的是4.3.6。 |
|
嗯,用的是4.3.6 |
|
嗯 |
|
5分 |
使用后一种写法吧,这个问题当初stackoverflow中有人提出过
|
5分 |
估计你得hibernate版本比较老了 |