hibenate 4.0 이상 의 hibenate Util 도구 류

1203 단어 notes
/**
 * 
 */
package com.jikexueyuan.util;


import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;

/**
 * @author Sun Lianwei
 *    
 */
public class HibernateUtils {

	private static SessionFactory sessionFactory;
	private static Session session;
	
	static{
		Configuration cfg=new Configuration().configure();
		
		StandardServiceRegistryBuilder ssrb=new StandardServiceRegistryBuilder().applySettings(cfg.getProperties());
		ServiceRegistry service=ssrb.build();
		sessionFactory=cfg.buildSessionFactory(service);
	
	}
	
	private HibernateUtils(){
		
	}
	
//	public static SessionFactory getSessionFactory(){
//		return sessionFactory;
//	}
	/**
	 *   session  
	 */
	public static Session getSession(){
		return sessionFactory.openSession();
	}
	/**
	 *   session  
	 */
	public static void closeSession(){
		if(session!=null&&session.isOpen()){
			session.close();
		}
	}
	
	public static void closeSession(Session session){
		if(session!=null&&session.isOpen()){
			session.close();
		}
	}
}

좋은 웹페이지 즐겨찾기