【问题标题】:Proper use of session(hibernate)正确使用会话(休眠)
【发布时间】:2015-03-31 10:34:29
【问题描述】:

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

这是 HibernateUtil

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);
                }
            }
        }

该类使所有操作 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();
        }

【问题讨论】:

    标签: java hibernate session dao


    【解决方案1】:

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

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

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

    会话是轻量级的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-29
      • 2012-06-02
      • 1970-01-01
      • 2018-09-27
      • 2013-09-14
      • 1970-01-01
      相关资源
      最近更新 更多