Java 如何从servlet中初始化hibernate会话?

Java 如何从servlet中初始化hibernate会话?,java,hibernate,servlets,Java,Hibernate,Servlets,我正在学习Javaservlet和hibernate。我有各自的工作示例,现在正试图将hibernate示例合并到HTTPservlet中 我的简单hibernate示例从以下代码开始 factory = new AnnotationConfiguration().configure().buildSessionFactory(); Session session = factory.openSession(); 当通过http get调用http servlet时,我知道它首先调用构造函数,

我正在学习Javaservlet和hibernate。我有各自的工作示例,现在正试图将hibernate示例合并到HTTPservlet中

我的简单hibernate示例从以下代码开始

factory = new AnnotationConfiguration().configure().buildSessionFactory();
Session session = factory.openSession();
当通过http get调用http servlet时,我知道它首先调用构造函数,然后调用doGet方法


现在我的问题是(我是一个新手,请对我说得轻松一点):从servlet调用hibernate初始化代码的公认方式是什么?我是否将上述初始化代码放入构造函数方法中?

有很多方法可以管理hibernate会话。最常用的方法之一是使用
HibernateUtil
class。通常的实现是,
SessionFactory
是静态初始化的,这意味着初始化只会执行一次(当类被加载时)。然后,它公开了一个获取构建的
SessionFactory
实例的静态方法。下面是dzone的一个实现示例

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateUtil {
    private static final SessionFactory sessionFactory;
    static {
        try {
            sessionFactory = new Configuration().configure()
                    .buildSessionFactory();
        } catch (Throwable ex) {
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }
}

对于会话的打开和关闭,它通常被视为请求处理的一个单元,因此您可以调用
HibernateUtil.getSessionFactory().openSession()
doGet
doPost
的开头,并确保在方法结束前关闭会话。

为Hibernet连接创建一个单独的类

    package com.abc.xyz.dao;

    import org.hibernate.SessionFactory;
    import org.hibernate.cfg.AnnotationConfiguration;


    public class HibernateUtil {

      private static final SessionFactory sessionFactory = buildSessionFactory();

      @SuppressWarnings("deprecation")
    private static SessionFactory buildSessionFactory() {
        try {
          // Create the SessionFactory from Annotation
          return new AnnotationConfiguration().configure().buildSessionFactory();
        }
        catch (Throwable ex) {
          // Make sure you log the exception, as it might be swallowed
          System.err.println("Initial SessionFactory creation failed." + ex);
          throw new ExceptionInInitializerError(ex);
        }
      }

      public static SessionFactory getSessionFactory() {
        return sessionFactory;
      }
    }
在服务器端获取该连接

        Session session=null;

     try {
        session =HibernateUtil.getSessionFactory().openSession();



    } catch (HibernateException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }finally
    {
        session.close();
    }

尝试使用servlet listener。

如果希望hibernate会话对象位于doGet或doPost方法,通常可以在其
init()
方法中或在
ServletContextListener
中执行。您应该在init方法中创建会话对象。并在类作用域中声明会话对象。请将您的答案再扩展一点好吗?谢谢!我尝试实现了这一点,但在doGet()中调用openSession()时出现错误“tomcat7 at localhost failed to start”,因为HibernateUtil类中的SessionFactory初始化代码似乎没有被调用。我做错了什么?