正确使用session(hibernate)

Proper use of session(hibernate)

对不起我的英语。我学习 JavaEE,但我不知道在休眠中使用会话是否合适。如何使用它们?我使用模式 DAOhibernate。告诉我 属性 如何使用会话

这是 HibernateUtil class

private static final SessionFactory sessionFactory;

    static {
        try{
            sessionFactory = new Configuration().configure("/app/web/landingpage/HibernateConnect/hibernate.cfg.xml").buildSessionFactory();
        }catch(Throwable ex) {
            System.out.println("Error " + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }

     public static void close(Session session) {
            if (session != null) {
                try {
                    session.close();
                } catch (HibernateException ignored) {
                    System.out.print("Couldn't close Session" + ignored);
                }
            }
        }

并且 class 使所有操作 db CategoryDaoImpl

    public class CategoryDaoImpl implements CategoryDao{
    private Session session = null;
    //get all category
    public Collection getAllCategory() {
            List categoris = new ArrayList<Category>();
            try{
                session = HibernateUtil.getSessionFactory().openSession();
                categoris = session.createCriteria(Category.class).list();
            }catch(Exception e) {
                System.out.println("getAllCategory "+ e);
            }finally{
                if(session != null && session.isOpen())
                    session.close();
            }


        return categoris;
            }
    //get category id
    public Category getCategory(int id) {

            Category cat = null;
            try {
                session = HibernateUtil.getSessionFactory().openSession();
                cat = (Category) session.load(Category.class, id);
            }catch(Exception e) {
                System.out.println("getAllCategory "+ e);
            }finally{
                if(session != null && session.isOpen())
                    session.close();
            }
            return cat;
        }

//and below few methods that use it the some way session
        }

此 servlet 获取结果 indexuser

Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();

try{
            Collection allcategory = Factory.getInstance().getCatDAO().getAllCategory();
request.setAttribute("allcategory", allcategory);
request.getRequestDispatcher("/index.jsp").forward(request, response); 
        } catch(Exception e) { 
            System.out.println(e);
        } finally{
            if(session!=null && session.isOpen())
                session.close();
        }

这里的主要约定是Session实例的创建。通常一个应用程序有一个 SessionFactory 实例,服务客户端请求的线程从这个工厂获取 Session 实例。

SessionFactory 的内部状态是不可变的。一旦它被创建,这个内部状态就会被设置。此内部状态包括有关 Object/Relational 映射的所有元数据。

基本上session是用来获取与数据库的物理连接的。因此,当您执行任何数据库操作时,它将首先使用 sessionFactory 打开会话,然后会话与数据库进行物理连接,然后执行您的操作,执行操作后您可以关闭它。

会话是轻量级的。