首页 > 代码库 > 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学习