Hibernate SessionFactory is the factory class through which we get sessions and perform database operations.

Hibernate SessionFactory是工厂类,通过它我们可以获取会话并执行数据库操作。

Hibernate会话工厂 (Hibernate SessionFactory)

Hibernate SessionFactory provides three methods through which we can get Session object – getCurrentSession(), openSession() and openStatelessSession().

Hibernate SessionFactory提供了三种获取Session对象的方法: getCurrentSession()openSession()openStatelessSession()

HibernateSessionFactory getCurrentSession (Hibernate SessionFactory getCurrentSession)

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配置文件中对其进行配置,如下所示。

<property name="hibernate.current_session_context_class">thread</property>

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对象不是线程安全的,因此我们不应在多线程环境中使用它。 我们可以在单线程环境中使用它,因为它比打开新会话相对更快。

HibernateSessionFactory openSession (Hibernate SessionFactory openSession)

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应用程序框架,我们可以根据需要选择为每个请求或每个会话打开一个新会话。

HibernateSessionFactory openStatelessSession (Hibernate SessionFactory openStatelessSession)

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 closeSession currentSession = sessionFactory.getCurrentSession();//open new sessionSession newSession = sessionFactory.openSession();//perform db operations//close sessionnewSession.close();//open stateless sessionStatelessSession statelessSession = sessionFactory.openStatelessSession();//perform stateless db operations//close sessionstatelessSession.close();//close session factorysessionFactory.close();}}

That’s all for SessionFactory in Hibernate and it’s different methods to obtain session object.

这就是Hibernate中SessionFactory的全部内容,并且它是获取会话对象的不同方法。

翻译自: https://www.journaldev.com/3522/hibernate-sessionfactory

Hibernate会话工厂相关推荐

  1. hibernate的映射关系配置及对会话工厂的初始化。以及struts2写实例查询

    1.首先获取hibernate的jar导入,不写. 2.hibernate关键配置映射文件有两个,关键工具一个 分别是: 核心配置 hibernate.cfg.xml 持久化类对象与数据库映射配置*. ...

  2. 总结xml配置spring-aop声明式事务配置与hibernate报错:** isno active spring和hibernate整合,原因会话工厂去路(到spring不仅仅是bean)错误

    spring事务管理太厉害了!!可以不再自管事务开发了! spring aop声明式事务配置 问题: 困扰我近十多天的的spring事务管理终于解决了, 再也不用自己管理事务了 嗯,可以删该死的hib ...

  3. Hibernate api 之常见的类(配置类,会话工厂类,会话类)

    1:Configuration :配置管理类对象 1.1:config.configure(): 加载主配置文件的方法(hibernate.cfg.xml) ,默认加载src/hibernate.cf ...

  4. shiro的会话工厂SessionFactory

    SessionFactory是创建会话的工厂,根据相应的Subject上下文信息来创建会话:默认提供了SimpleSessionFactory用来创建SimpleSession会话. public i ...

  5. MyBatis关键配置-创建会话工厂

    Spring 对MyBatis 的对象进行了管理,但是并不会替换MyBatis 的核心对象.也就意味着:MyBatis jar 包中的SqlSessionFactory.SqlSession.Mapp ...

  6. Spring ORM数据訪问——Hibernate

    Hibernate 我们将首先介绍Spring环境中的Hibernate 5.然后介绍使用Hibernate 5来演示Spring集成O-R映射器的方法. 本节将具体介绍很多问题,并显示DAO实现和事 ...

  7. Hibernate注解使用以及Spring整合

    (1) 简介: 在过去几年里,Hibernate不断发展,几乎成为Java数据库持久性的事实标准.它非常强大.灵活,而且具备了优异的性能.在本文中,我们将了解如何使用Java 5 注释来简化Hiber ...

  8. hibernate 别名_Hibernate:在sqlRestriction上使用联接表别名

    hibernate 别名 如果在复杂查询的情况下使用Hibernate模式,则需要使用sql. 因此,sqlRestrictions可以解决. 但是,对联接表别名使用sql限制有点棘手. 将有三个表: ...

  9. Hibernate面试问题与解答

    Hibernate面试问题与解答 Hibernate是Java应用程序中使用最广泛的ORM工具之一.它在企业应用程序中用于数据库操作.所以我决定写一篇关于的帖子 hibernate面试问题,在面试前刷 ...

最新文章

  1. 用了很多年Dubbo,连Dubbo线程池监控都不知道,觉得自己很厉害?
  2. 天猫上线“商家售后服务评价”功能,消费者体验将纳入商家考核指标
  3. love2d杂记6--动态读写外部lua文件
  4. 自动关机脚本2007-10-28 10:04@ECHO off
  5. 大数据相关从业_如何在组织中以数据从业者的身份闪耀
  6. 前端学习(2711):重读vue电商网站31之左侧菜单栏图标设计
  7. 记录一次游戏服务器的压测调优记录(Golang语言)
  8. Ubuntu16.04编译Android5.1源码
  9. 入门学Qt_软件Demo界面GUI设计流程综述
  10. asp php 一句话,ASP_asp一句话木马原理分析,通过HTTP协议来访问 一句话木 - phpStudy...
  11. 如何用活字格定制监狱管理系统
  12. GHOST恢复盘.维护盘关键词
  13. Python代码编辑器jupyter的使用
  14. Elasticsearch 使用初级入门 【入门篇】
  15. image图片大小调整和方向调整(UIImageOrientation)
  16. 多元线性回归分析spss结果解读_SPSS经典线性回归分析之一——线性回归分析
  17. python做一副54扑克牌发牌技巧_最强大脑!杭四中高一男生邹全50秒轻松记住一副扑克牌...
  18. 计算机视觉寒假实习生面经
  19. skyline开发:TE弹出窗口和主页面的交互
  20. 网络安全等保定级_信息安全技术网络安全等级保护定级指南发布,2020年11月1日正式实施!...

热门文章

  1. Java SE 6生命将在今年11月终结
  2. Ruby笔记三(类、对象、属性)
  3. 在 Linux 上部署 Django 应用,nginx+gunicorn+supervisor
  4. [转载] Python 字典删除元素clear、pop、popitem
  5. try catch finally的理解
  6. uniapp 开发踩坑记录
  7. CF EDU - E. Lomsat gelral 树上启发式合并
  8. 十进制数转N进制c++实现
  9. 开发常用技巧之css字体编码
  10. 利用UICollectionView实现瀑布流