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!
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。