现在的位置: 首页 > 综合 > 正文

Hibernate系列之—-Hibernate4.1.5 buildSessionFactory过时

2018年02月13日 ⁄ 综合 ⁄ 共 704字 ⁄ 字号 评论关闭

package com.dev.hibernate;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

public class HibernateUtils
{

	private static SessionFactory factory;

	static
	{
		Configuration cfg = new Configuration().configure();
		ServiceRegistry sr = new ServiceRegistryBuilder().applySettings(
				cfg.getProperties()).buildServiceRegistry();
		factory = cfg.buildSessionFactory(sr);
	}

	public static SessionFactory getSessionFactory()
	{
		return factory;
	}

	public static Session getSession()
	{
		return factory.openSession();
	}

	public static void closeSession(Session session)
	{
		if (session != null)
		{
			if (session.isOpen())
			{
				session.close();
			}
		}
	}
}

抱歉!评论已关闭.