日期:2014-05-17  浏览次数:20772 次

没有学过Hibernate,麻烦解释一下这段代码!
package com.news.server.hibernate;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;
/**
 * Configures and provides access to Hibernate sessions, tied to the
 * current thread of execution.  Follows the Thread Local Session
 * pattern, see {@link http://hibernate.org/42.html }.
 */
public class HibernateSessionFactory {

    /** 
     * Location of hibernate.cfg.xml file.
     * Location should be on the classpath as Hibernate uses  
     * #resourceAsStream style lookup for its configuration file. 
     * The default classpath location of the hibernate config file is 
     * in the default package. Use #setConfigFile() to update 
     * the location of the configuration file for the current session.   
     */
    private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";
private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
    private  static Configuration configuration = new Configuration();
    private static org.hibernate.SessionFactory sessionFactory; 
    private static String configFile = CONFIG_FILE_LOCATION;
   
    
/**
 * Returns the ThreadLocal Session instance. Lazy initialize the
 * <code>SessionFactory</code> if needed.
 * 
 * @return Session
 * @throws HibernateException
 */
public static Session currentSession() throws HibernateException {
Session session = (Session) threadLocal.get();

if (session != null && !session.isOpen())
session = null;
if (session == null)
{
if (sessionFactory == null)
{
try
{
configuration.configure(CONFIG_FILE_LOCATION);
sessionFactory = configuration.buildSessionFactory();

} catch (Exception e)
{
System.err.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}
session = sessionFactory.openSession();
threadLocal.set(session);
}

return session;
}

    private HibernateSessionFactory() {
    }

/**
     * Return