sessionfactory.java

来自「A Java web application, based on Struts 」· Java 代码 · 共 62 行

JAVA
62
字号
/* @LICENSE_COPYRIGHT@ */
package net.sf.irunninglog.hibernate;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

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

import net.sf.irunninglog.util.FatalRuntimeException;

/**
 * Factory class used for creating Hibernate sessions.  This class encapsulates
 * all of the logic needed to configure Hibernate and instantiate new session
 * instances using the static <code>openSession</code> method.
 *
 * @author <a href="mailto:allan_e_lewis@yahoo.com">Allan Lewis</a>
 * @version $Revision: 1.2 $ $Date: 2005/06/25 15:18:20 $
 * @since iRunningLog 1.0
 */
public final class SessionFactory {

    /** <code>Log</code> instance for this class. */
    private static final Log LOG = LogFactory.getLog(SessionFactory.class);

    /** Factory for creating sessions. */
    private static final org.hibernate.SessionFactory FACTORY;

    static {
        try {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Configuring Hibernate");
            }

            FACTORY = new Configuration().configure().buildSessionFactory();
        } catch (Throwable ex) {
            LOG.fatal("Error encountered while configuring Hibernate", ex);
            throw new FatalRuntimeException(ex);
        }
    }

    /** Utility class - does not expose a constructor. */
    private SessionFactory() { }

    /**
     * Create and open a new Hibernate session.  This will use the session
     * factory to provide a new session for use in Hibernate operations.
     *
     * @return The new Hibernate session
     */
    protected static Session openSession() {
        Session session = FACTORY.openSession();

        if (LOG.isDebugEnabled()) {
            LOG.debug("openSession: Created a new session " + session);
        }

        return session;
    }

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?