博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Hibernate会话工厂
阅读量:2531 次
发布时间:2019-05-11

本文共 4841 字,大约阅读时间需要 16 分钟。

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

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

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 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/

你可能感兴趣的文章
小D课堂 - 新版本微服务springcloud+Docker教程_4-02 微服务调用方式之ribbon实战 订单调用商品服务...
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_4-03 高级篇幅之Ribbon负载均衡源码分析实战...
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_4-06 Feign核心源码解读和服务调用方式ribbon和Feign选择...
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_4-05 微服务调用方式之feign 实战 订单调用商品服务...
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_5-02 Netflix开源组件断路器
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_5-01分布式核心知识之熔断、降级
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_5-04 feign结合hystrix断路器开发实战下...
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_5-03 feign结合hystrix断路器开发实战上...
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_6-01 微服务网关介绍和使用场景
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_5-05熔断降级服务异常报警通知
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_6-03 高级篇幅之zuul常用问题分析
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_5-08 断路器监控仪表参数
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_6-02 springcloud网关组件zuul
查看>>
小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_2-1.快速搭建SpringBoot项目,采用Eclipse...
查看>>
小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_1-4.在线教育后台数据库设计...
查看>>
小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_2-3.热部署在Eclipse和IDE里面的使用...
查看>>
小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_1-3.在线教育站点需求分析和架构设计...
查看>>
小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_2-4.后端项目分层分包及资源文件处理...
查看>>
小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_2-2.快速搭建SpringBoot项目,采用IDEA...
查看>>
小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_3-5.PageHelper分页插件使用
查看>>