我如何从 DAO 中的 ServletContext 获取 SessionFactory

How would I obtain SessionFactory from the ServletContext in the DAO

我创建了一个这样的 DAO:这是基于:Hibernate: CRUD Generic DAO

public class Dao{
    SessionFactory sessionFactory;
    // initialise session factory
    public User save(User o){
        return (User) sessionFactory.getCurrentSession().save(o);
    }

    public User get(Long id){
        return (User) sessionFactory.getCurrentSession().get(User.class, id);
    }

    public User void saveOrUpdate(User o){
                    sessionFactory.getCurrentSession().saveOrUpdate(o);
    }

现在,如果我的 sessionFactory 在 DAO 或其他 类 中,这一切都很好。但我的问题是从 servletContextListener 调用 SessionFactory:这是我在监听器中的代码:

public void contextInitialized(ServletContextEvent event)  {
    StandardServiceRegistry registry = new StandardServiceRegistryBuilder().configure().build();
    try {
        sessionFactory = new MetadataSources(registry).buildMetadata().buildSessionFactory();
        event.getServletContext().setAttribute("factory", sessionFactory);
    } catch(Exception e) {
        e.printStackTrace();
        StandardServiceRegistryBuilder.destroy( registry );
    }
}

在这种情况下,除了在 DAO 中实际包装 servletRequest 之外,我如何从 DAO 调用 SessionFactory?

我强烈不鼓励 将 Hibernate SessionFactory 存储到 Servlet 的 ServletContext 中。另外,让你的 DAO 使用你的 Hibernate Session

为了解决您的 Hibernate SessionFactory 实例化一次的问题,我创建了一个单例 class 来管理它:

/**
 * 
 */
package za.co.sindi.persistence.util;

import java.util.logging.Level;
import java.util.logging.Logger;

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

/**
 * This is a Hibernate utility class that strictly uses the Hibernate 4.x library.
 * 
 * @author Buhake Sindi
 * @since 26 November 2012
 *
 */
public final class HibernateUtils {

    private static final Logger logger = Logger.getLogger(HibernateUtils.class.getName());
    private static Configuration configuration;
    private static SessionFactory sessionFactory;
    private static ServiceRegistry serviceRegistry;
    private static final ThreadLocal<Session> sessionThread = new ThreadLocal<Session>();
    private static final ThreadLocal<Interceptor> interceptorThread = new ThreadLocal<Interceptor>();

    static {
        try {
            configuration = new Configuration();
            serviceRegistry = new StandardServiceRegistryBuilder().build();
            sessionFactory = configuration.configure().buildSessionFactory(serviceRegistry);
        } catch (HibernateException e) {
            logger.log(Level.SEVERE, "Error intializing SessionFactory.", e.getLocalizedMessage());
            throw new ExceptionInInitializerError(e);
        }
    }

    /**
     * Private constructor
     */
    private HibernateUtils() {}

    /**
     * @return the sessionFactory
     */
    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    /**
     * Retrieves the current session local to the thread.
     * 
     * @return Hibernate {@link Session} for current thread.
     * @throws HibernateException when Hibernate has a problem opening a new session.
     */
    public static Session getSession() {
        Session session = sessionThread.get();
        if (session == null) {
            Interceptor interceptor = getInterceptor();
            if (interceptor != null) {
                session = getSessionFactory().withOptions().interceptor(interceptor).openSession();
            } else {
                session = getSessionFactory().openSession();
            }

            if (session != null) {
                sessionThread.set(session);
            }
        }

        return session;
    }

    /**
     * Closes the Hibernate Session (created from the <code>getSession()</code> session.
     */
    public static void closeSession() {
        Session session = sessionThread.get();
        sessionThread.set(null);
        if (session != null && session.isOpen()) {
            session.close();
        }
    }

    /**
     * Registers a Hibernate {@link Interceptor}.
     * @param interceptor
     */
    public static void registerInterceptor(Interceptor interceptor) {
        interceptorThread.set(interceptor);
    }

    /**
     * Get the registered Hibernate Interceptor.
     * @return
     */
    public static Interceptor getInterceptor() {
        return interceptorThread.get();
    }
}

在我的 DAO 中,我只是将会话检索为 HibernateUtils.getSession();。 这样,我的 MVC 应用程序就不会引用我的特定 DAO 实现。

希望对您有所帮助。