Hibernate 工具类

package com.newer;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;


/**
 * HibernateSessionFactory 工具类 <br/>
 * 此类用于获取 sessionFactory、session等
 */

public class HibernateSessionFactory {

    private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";  //位置classPath里面: 

    private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
    private static org.hibernate.SessionFactory sessionFactory;
    private static String configFile = CONFIG_FILE_LOCATION;
    
   //private  static Configuration configuration = new Configuration(); //XML配置时使用
    private  static Configuration configuration = new AnnotationConfiguration(); //@注解配置时使用

    static {
              try {
                     configuration.configure(configFile);
                     sessionFactory = configuration.buildSessionFactory();
              } catch (Exception e) {
                     System.err.println("%%%% Error Creating SessionFactory %%%%");
                     e.printStackTrace();
             }
    }
 
   /**
     * 构造方法
     */
    private HibernateSessionFactory() {

    }
 

    /**
      * 获取Session的方法 <br/>
      * session使用完后应 close()释放掉<br/>
      * 因为每个session 都会带上一个数据库连接
      * @return
      */
    public static Session getSession() throws HibernateException {
            Session session = (Session) threadLocal.get();

            if (session == null || !session.isOpen()) {
                     if (sessionFactory == null) {
                                    rebuildSessionFactory();
                     }
                     session = (sessionFactory != null) ? sessionFactory.openSession() : null;
                     threadLocal.set(session);
            }

            return session;
    }

    public static void rebuildSessionFactory() {
            try {
                    configuration.configure(configFile);
                    sessionFactory = configuration.buildSessionFactory();
            } catch (Exception e) {
                    System.err.println("%%%% Error Creating SessionFactory %%%%");
                    e.printStackTrace();
            }
    }

    public static void closeSession() throws HibernateException {
            Session session = (Session) threadLocal.get();
            threadLocal.set(null);

            if (session != null) {
                    session.close();
            }
    }

    /**
     * 获取sessionFactory的方法
     * @return
     */
    public static org.hibernate.SessionFactory getSessionFactory() {
               return sessionFactory;
    }

    /**
     * 清空 sessionFactory 的方法
     * @return
     */
    public static void setConfigFile(String configFile) {
              HibernateSessionFactory.configFile = configFile;
              sessionFactory = null;
    }

    /**
     * 获取 configuration 的方法
     * @return
     */
   public static Configuration getConfiguration() {
             return configuration;
   }

} 

你可能感兴趣的:(Hibernate 工具类)