hibernate解读之session--基于最新稳定版5.2.12

前言


hibernate是一个实现了JPA标准的,用于对象持久化的orm框架。博主近一年开发都在使用。

前段时间在工作中遇到了一个hibernate的问题,从数据库查找得到对象后,修改了其中部分字段值之后没保存(没有调用merge),再以同样的方式从数据库查找刚才的对象,发现其中的属性居然已经发生了变化(与数据库不符,并且控制台打印出了hibernate的查询日志)。

想到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的内部情况可能和数据库不一致。

题外话


之前有人问,web开发中也有个session,这两个session有什么关系?

其实,servlet 中的Session也是缓存,缓存你与服务器对话时候的一些信息。

总之所有的session基本都是起缓存作用的,就是把一些信息缓存在内存中,方便存取值。

但是不同的session是不能相互直接赋值的,因为是两个不同的概念,只是名字一样。(让我想起了网上的段子,java和JavaScript有啥区别,周杰和周杰伦有啥区别 -。-)

名字一样也是为了帮助程序员理解,学了servlet的session 后再遇到session也就应该是缓存作用的。

欢迎讨论,共同学习! 码字排版不易,转载请在文章明显处注明出处并附原文链接!
原文地址:https://www.cnblogs.com/optblogs/p/8400131.html