package cn.updev.Database;

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

/**
 * Created by hypo on 15-11-17.
 */
public class HibernateSessionFactory {

    private static String CONFIG_FILE_LOCATION = "hibernate.cfg.xml";

    /** Holds a single instance of Session */
    private static final ThreadLocal threadLocal = new ThreadLocal();

    /** The single instance of hibernate configuration */
    private static final Configuration cfg = new Configuration();

    /** The single instance of hibernate SessionFactory */
    private static org.hibernate.SessionFactory sessionFactory;

    /**
     * Returns the ThreadLocal Session instance. Lazy initialize
     * the SessionFactory if needed.
     *
     * @return Session
     * @throws HibernateException
     */

    public static Session currentSession() throws HibernateException {
        Session session = (Session) threadLocal.get();

        if (session == null) {
            if (sessionFactory == null) {
                try {
                    cfg.configure(CONFIG_FILE_LOCATION);
                    sessionFactory = cfg.buildSessionFactory();
                }
                catch (Exception e) {
                    System.err.println("%%%% Error Creating SessionFactory %%%%");
                    e.printStackTrace();
                }
            }
            session = sessionFactory.openSession();
            threadLocal.set(session);
        }

        return session;
    }

    /**
     * Close the single hibernate session instance.
     *
     * @throws HibernateException
     */
    public static void closeSession() throws HibernateException {
        Session session = (Session) threadLocal.get();
        threadLocal.set(null);

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

    /**
     * Default constructor.
     */
    private HibernateSessionFactory() {
    }

}

@@ -21,16 +21,48 @@
 abase;%0A%0A
+import org.apache.log4j.Logger;%0A
 import o
@@ -90,24 +90,24 @@
 eException;%0A
-
 import org.h
@@ -682,16 +682,118 @@
 ctory;%0A%0A
+    /** Log4j */%0A    private static Logger logger = Logger.getLogger(HibernateSessionFactory.class);%0A%0A
     /**%0A
@@ -1390,48 +1390,24 @@
     
-System.err.println(%22%25%25%25%25 Error Creating 
+logger.error(%22%E8%8E%B7%E5%BE%97
 Sess
@@ -1420,13 +1420,11 @@
 tory
- %25%25%25%25
+%E5%A4%B1%E8%B4%A5!
 %22);%0A

add log4g on HiberanteSessionFactory