首页 > 代码库 > hibernate 4.3.7.Final学习
hibernate 4.3.7.Final学习
异常:
[Solved] HibernateException: Access to DialectResolutionInfo cannot be null when ‘hibernate...
源码:
public SessionFactory buildSessionFactory() throws HibernateException { Environment.verifyProperties( properties ); ConfigurationHelper.resolvePlaceHolders( properties ); final ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder() .applySettings( properties ) .build(); setSessionFactoryObserver( new SessionFactoryObserver() { @Override public void sessionFactoryCreated(SessionFactory factory) { } @Override public void sessionFactoryClosed(SessionFactory factory) { ( (StandardServiceRegistryImpl) serviceRegistry ).destroy(); } } ); return buildSessionFactory( serviceRegistry ); }
我获取sessionFactory的方式:
package util; import org.hibernate.SessionFactory; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.cfg.Configuration; import org.hibernate.service.ServiceRegistry; /** * @author guozhenzhen * */ public class HibernateUtil { private static final SessionFactory sf = buildHibernateSessionFactory(); private static SessionFactory buildHibernateSessionFactory() { Configuration cfg = new Configuration().configure(); ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder() .build(); return cfg.buildSessionFactory(serviceRegistry); } public static SessionFactory getSessionFactory() { return sf; } }
可以看得出来是少了一个applySettings方法,把属性都设置进去。
修改如下以后,hibernate可以正常工作了:
package util; import org.hibernate.SessionFactory; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.cfg.Configuration; import org.hibernate.service.ServiceRegistry; /** * @author guozhenzhen * */ public class HibernateUtil { private static final SessionFactory sf = buildHibernateSessionFactory(); private static SessionFactory buildHibernateSessionFactory() { Configuration cfg = new Configuration().configure(); ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder() .applySettings(cfg.getProperties()).build(); return cfg.buildSessionFactory(serviceRegistry); } public static SessionFactory getSessionFactory() { return sf; } }
继续运行hibernate小例子:
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
出现异常:No CurrentSessionContext configured!
hibernate 4.3.7.Final学习
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。