简体   繁体   English

如何正确使用hibernate-sessions?

[英]How to use hibernate-sessions properly?

From what I have read you create a session per request and close the session after the request has finished. 根据我的阅读,您可以为每个请求创建一个会话,并在请求完成后关闭会话。

Would this be right usage of sessions in hibernate? 这是hibernate会话的正确用法吗?

  1. start server and create SessionFactory 启动服务器并创建SessionFactory
  2. user want to query data from database 2.1 Session userSession = MyHibernateClass.getFactory().openSession(); 用户想要从数据库查询数据2.1 Session userSession = MyHibernateClass.getFactory().openSession();
  3. do one query 做一个查询
  4. session.close();

When do I use getCurrentSession() and when openSession() ? 我什么时候使用getCurrentSession()openSession()

SessionFactory.openSession() 

always opens a new session that you have to close once you are done with the operations. 总是会打开一个新的会话,一旦完成操作,您必须关闭该会话。 SessionFactory.getCurrentSession() returns a session bound to a context - you don't need to close this. SessionFactory.getCurrentSession()返回绑定到上下文的会话 - 您不需要关闭它。 For more detailed information you can follow given link openSession() v/s getCurrentSession() 有关更多详细信息,您可以按照给定的链接openSession()v / s getCurrentSession()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM