本文共 4841 字,大约阅读时间需要 16 分钟。
Hibernate SessionFactory is the factory class through which we get sessions and perform database operations.
Hibernate SessionFactory是工厂类,通过它我们可以获取会话并执行数据库操作。
Hibernate SessionFactory provides three methods through which we can get Session object – getCurrentSession()
, openSession()
and openStatelessSession()
.
Hibernate SessionFactory提供了三种获取Session对象的方法: getCurrentSession()
, openSession()
和openStatelessSession()
。
Hibernate SessionFactory
getCurrentSession() method returns the session bound to the context. But for this to work, we need to configure it in hibernate configuration file like below.
Hibernate SessionFactory
getCurrentSession()方法返回绑定到上下文的会话。 但是要使其正常工作,我们需要在Hibernate配置文件中对其进行配置,如下所示。
thread
If its not configured to thread, then we will get below exception.
如果未将其配置为线程,那么我们将获得以下异常。
Exception in thread "main" org.hibernate.HibernateException: No CurrentSessionContext configured! at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:1012) at com.journaldev.hibernate.main.HibernateSessionExample.main(HibernateSessionExample.java:16)
Since this session object belongs to the hibernate context, we don’t need to close it. Once the session factory is closed, this session object gets closed.
由于此会话对象属于Hibernate上下文,因此我们不需要关闭它。 会话工厂关闭后,该会话对象将关闭。
Hibernate Session objects are not thread safe, so we should not use it in multi-threaded environment. We can use it in single threaded environment because it’s relatively faster than opening a new session.
Hibernate Session对象不是线程安全的,因此我们不应在多线程环境中使用它。 我们可以在单线程环境中使用它,因为它比打开新会话相对更快。
Hibernate SessionFactory
openSession() method always opens a new session. We should close this session object once we are done with all the database operations.
Hibernate SessionFactory
openSession()方法总是打开一个新的会话。 一旦完成所有数据库操作,就应该关闭该会话对象。
We should open a new session for each request in multi-threaded environment. For web application frameworks, we can choose to open a new session for each request or for each session based on the requirement.
我们应该在多线程环境中为每个请求打开一个新会话。 对于Web应用程序框架,我们可以根据需要选择为每个请求或每个会话打开一个新会话。
Hibernate SessionFactory
openStatelessSession() method returns instance of StatelessSession
. There is another overloaded method where we can pass java.sql.Connection
object to get a stateless session object from hibernate.
Hibernate SessionFactory
openStatelessSession()方法返回StatelessSession
实例。 还有另一个重载方法,我们可以传递java.sql.Connection
对象从Hibernate中获取无状态会话对象。
StatelessSession in Hibernate does not implement first-level cache and it doesn’t interact with any second-level cache. Since it’s stateless, it doesn’t implement transactional write-behind or automatic dirty checking or do cascading operations to associated entities.
Hibernate中的StatelessSession不实现第一级缓存,并且不与任何第二级缓存交互。 由于它是无状态的,因此不会实现事务后写或自动脏检查或对关联实体进行级联操作。
Collections are also ignored by a stateless session. Operations performed via a stateless session bypass Hibernate’s event model and interceptors. It’s more like a normal JDBC connection and doesn’t provide any benefits that come from using hibernate framework.
无状态会话也会忽略集合。 通过无状态会话执行的操作会绕过Hibernate的事件模型和拦截器。 它更像是一个普通的JDBC连接,并且没有提供使用Hibernate框架带来的任何好处。
However, stateless session can be a good fit in certain situations. For example where we are loading bulk data into database and we don’t want hibernate session to hold huge data in first-level cache memory.
但是,无状态会话在某些情况下可能非常适合。 例如,当我们将大量数据加载到数据库中时,我们不希望Hibernate会话将大量数据保存在一级缓存中。
A simple program showing Hibernate SessionFactory methods usage is given below.
下面给出一个显示Hibernate SessionFactory方法用法的简单程序。
package com.journaldev.hibernate.main;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.StatelessSession;import com.journaldev.hibernate.util.HibernateUtil;public class HibernateSessionExample { public static void main(String[] args) { SessionFactory sessionFactory = HibernateUtil.getSessionFactory(); //Current Session - no need to close Session currentSession = sessionFactory.getCurrentSession(); //open new session Session newSession = sessionFactory.openSession(); //perform db operations //close session newSession.close(); //open stateless session StatelessSession statelessSession = sessionFactory.openStatelessSession(); //perform stateless db operations //close session statelessSession.close(); //close session factory sessionFactory.close(); }}
That’s all for SessionFactory in Hibernate and it’s different methods to obtain session object.
这就是Hibernate中SessionFactory的全部内容,并且它是获取会话对象的不同方法。
翻译自:
转载地址:http://gdqzd.baihongyu.com/