hibernate解读之session--基于最新稳定版5.2.12
前言
hibernate是一个实现了JPA标准的,用于对象持久化的orm框架。博主近一年开发都在使用。
前段时间在工作中遇到了一个hibernate的问题,从数据库查找得到对象后,修改了其中部分字段值之后没保存(没有调用merge),再以同样的方式从数据库查找刚才的对象,发现其中的属性居然已经发生了变化。
想到hibernate默认具有一级缓存,觉得问题应该出在了这里。
hibernate的一级缓存就是session缓存。当要查询的数据在缓存中已经存在时,hibernate将不会再向数据库发送查询语句,而是直接从缓存中获取对象。
由此引发了对hibernate中session对象的思考,查看官方注释以及网上的一些资料,注释原文和自己手写的翻译都写在下面了,同学们可以自行选择阅读。
Persistence
持久化这个概念,算是老生常谈了。通俗的理解就是将数据保存到数据库中。在官网看到了这样一段话,觉得解释的特别贴切:
Persistence simply means that we would like our application’s data to outlive the applications process. In Java terms, we would like the state of (some of) our objects to live beyond the scope of the JVM so that the same state is available later.
简单来说,持久化意味着我们想让我们应用程序的数据的生命周期超过应用程序进程。用Java术语来说,我们想要我们的(某些)对象的状态超出JVM的范围,以便稍后可以使用相同的状态。
超出进程和JVM还能获取到数据,那肯定是保存在数据库咯。
session概述
The main runtime interface between a Java application and Hibernate. This is the central API class abstracting the notion of a persistence service.
session是一个在java应用和Hibernate之间的核心接口,是抽象持久化服务的核心API类。
The lifecycle of a Session is bounded by the beginning and end of a logical transaction. (Long transactions might span several database transactions.)
session的生命周期是由逻辑事务的开始和结束所限定的。(长事务可能跨越多个数据库事务。)
The main function of the Session is to offer create, read and delete operations for instances of mapped entity classes.
session的主要功能是对已经被映射到实体类的实例(对象)提供创建、读取和删除操作。
实例的三种状态
Instances may exist in one of three states:
1.transient: never persistent, not associated with any Session
2.persistent: associated with a unique Session
3.detached: previously persistent, not associated with any Session
实例的状态可能是下面三种之一:
1.瞬态:数据库无记录,也不在Session缓存中
2.持久态:数据库有记录,session缓存中也有,且与唯一的Session关联
3.游离态:数据库有记录,但不在session缓存中,但是不与任何Session关联
tips:之前看到有很多博客说实例存在4种状态,也说有3中的,说是hibernate低版本中说是3种,但是博主对比了3.6.10和最新稳定版5.2.12,发现两个写的都是3种状态。

状态间的转换
Transient instances may be made persistent by calling save(), persist() or saveOrUpdate().
Persistent instances may be made transient by calling delete().
Detached instances may be made persistent by calling update(), saveOrUpdate(), lock() or replicate().
瞬态-->持久态: 可以调用save(),persist(),或saveorupdate()
持久态-->瞬态: 可以调用delete()
游离态-->持久态:可以通过调用update(),saveorupdate(),lock()或replicate()。
Any instance returned by a get() or load() method is persistent.
注:通过Session的get()或load()方法返回的所有实例都是持久态。
The state of a transient or detached instance may also be made persistent as a new persistent instance by calling merge().
瞬时态或游离态的的实例也可以通过merge()方法,持久化成为一个新的持久态的实例。
对应在SQL中的操作
save() and persist() result in an SQL INSERT, delete() in an SQL DELETE and update() or merge() in an SQL UPDATE. Changes to persistent instances are detected at flush time and also result in an SQL UPDATE. saveOrUpdate() and replicate() result in either an INSERT or an UPDATE.
save()和persist()在SQL中是insert操作,delete()在SQL中是delete操作,update()或者merge()在SQL中是update操作。在刷新时间中持久态实例变为游离态的,在SQL中的结果也是update。saveorupdate()和replicate()的结果是insert操作或update操作。
It is not intended that implementors be threadsafe. Instead each thread/transaction should obtain its own instance from a SessionFactory.
这并不表示这种实现是线程安全的。每个线程/交易都应从SessionFactory获取自己的实例。
A Session instance is serializable if its persistent classes are serializable.
如果一个session实例的持久化实例是可序列化的,那么这个session实例也是可序列化的。
一个典型的事务的使用应该如下:

If the Session throws an exception, the transaction must be rolled back and the session discarded. The internal state of the Session might not be consistent with the database after the exception occurs.
如果这个Session抛出了异常,这个交易必须回滚!!并且废弃掉这个session。发生异常后,session的内部情况可能和数据库不一致。
hibernate解读之session--基于最新稳定版5.2.12的更多相关文章
- Hadoop介绍及最新稳定版Hadoop 2.4.1下载地址及单节点安装
Hadoop介绍 Hadoop是一个能对大量数据进行分布式处理的软件框架.其基本的组成包括hdfs分布式文件系统和可以运行在hdfs文件系统上的MapReduce编程模型,以及基于hdfs和MapR ...
- Android Studio最新稳定版下载 - 百度网盘(更新于2017年7月14日)
Android Studio是一个为Android平台开发程序的集成开发环境,其包含用于构建Android应用所需的所有工具.Android Studio 2.3.3为最新稳定版(截止到2017年7月 ...
- Ubuntu 14.04 安装最新稳定版Nginx 1.6.0
如果已经安装,请先卸载sudo apt-get remove nginx最新的稳定版Nginx 1.6.0在ubuntuupdates ppa库中提供,网址http://www.ubuntuupdat ...
- nvm安装最新稳定版node
安装当前最新的稳定版. nvm install stable
- centos7安装最新稳定版nginx
开始安装 yum 安装 nginx yum安装nginx文档地址 # 一切以最新的文档页面为准--搜centos http://nginx.org/en/linux_packages.html yum ...
- 2020年ubuntu1804安装nginx最新稳定版1.16详细教程笔记
第一次使用nginx是2007年,当时主流还是apache.nginx横空出世,在web2.0的推动下,迅速崛起.眼下已是绝对的主流了. 当时,还有一个轻量级的lighttpd,是德国人写,刚开始还并 ...
- 【Linux】Centos7 安装redis最新稳定版及问题解决
------------------------------------------------------------------------------------------------- | ...
- windows 10专业版14393.447 64位纯净无广告版系统 基于官方稳定版1607制作 更新于20161112
系统特点: 447更新日志(Win10 PC一周年更新正式版14393.447 32位/64位更新补丁KB3200970下载 Flash补丁Kb3202790下载): 1.通过网友的反馈,保留了Edg ...
- 查阅Springboot官方文档方式----------------Springboot2.0.2最新稳定版
1.登录官方网址: https://spring.io/ 如图所示: 2.选择PROJECTS,就可以看到spring所有的相关项目了. 点开后:其中就包括了Spingboot 3.版本选择,红圈部分 ...
随机推荐
- 基于JerseyToken安全设计
网上Jersey中文资料不多,更别提其他了.本人跟进项目具体需求弄了简单的api认证机制 基本流程图 后端登录退出代码: @Path("Account") public class ...
- for循环找出2到100的质数(素数)
思路: 1,一个数只有1和它本身两个因数,这个数叫质数. 2.注意:缩进这里else是for循环这个上下文的. 代码: for num in range(2,100): #为大循环变量num提供2-1 ...
- IdentityServer(15)- 第三方快速入门和示例
这些示例不由IdentityServer团队维护. IdentityServer团队提供链接到了社区示例,但不能对示例做任何保证. 如有问题,请直接与作者联系. 各种ASP.NET Core安全示例 ...
- UVALive 3882 - And Then There Was One【约瑟夫问题】
题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...
- [bzoj3124] [Sdoi2013]直径
看了child学长的题解才知道怎么写TAT http://www.cnblogs.com/ctlchild/p/5160272.html 以前不知道直径都是过重心的..代码改着改着就和标程完全一样了Q ...
- 最短路 spfa 算法 && 链式前向星存图
推荐博客 https://i.cnblogs.com/EditPosts.aspx?opt=1 http://blog.csdn.net/mcdonnell_douglas/article/deta ...
- WEB 小案例 -- 网上书城(一)
距离上次写博客有两周了吧,最多的原因就是自己期末考试了,上课没听就只能在期末狠狠的复习了,毕竟已经挂科了.当然还是因为自己懒吧!!!废话不多说开始我们今天的正题,网上书城! 一. 新建数据表(MySQ ...
- Ceph部署(一)集群搭建
背景 Ceph简介 Ceph是一个分布式存储,可以提供对象存储.块存储和文件存储,其中对象存储和块存储可以很好地和各大云平台集成.一个Ceph集群中有Monitor节点.MDS节点(可选,用于文件存储 ...
- 如何为图片添加热点链接?(map + area)
所谓图片热点链接就是为图片指定一个或多个区域以实现点击跳转到指定的页面.简单来说就是点击某一区域就能跳转到相应的页面,而无需点击整个图片才能跳转. 说到图片热点链接,我首先想到了map + area, ...
- JavaScript八张思维导图—编程风格
JS基本概念 JS操作符 JS基本语句 JS数组用法 Date用法 JS字符串用法 JS编程风格 JS编程实践 不知不觉做前端已经五年多了,无论是从最初的jQuery还是现在火热的Angular,Vu ...