1. 接下来编写个测试程序:
  2. 查看输出结果如下:
  3. 上图左边第二次select命令是在线程休眠期间执行的,可知save()命令在提交事务前并没有进行数据库操作。上面那句话“持久态对象在数据库中都有相对应的记录”让我们进入到这个误区的。
  4. 接着说一说其中究竟怎么操作的:Hibernate中提供Session级别的缓存,属于事务范围,且只存在于Session的生命周期中。当调用Session接口的save()、update()、saveorupdate()、get()等方法时,如果在Session的缓存中还不存在相应的对象,Hibernate就会把该对象加入到缓存中。Session接口提供两个管理缓存的方法。
  • ecivt(object obj):用于将某个对象从Session的缓存中清除。
  • clear():用于将一级缓存(Session级缓存)中的所有对象全部清除,但不包括操作中的对象。

值得一提的是flush()方法:强制更新缓存,并提交事务。(Flushing is the process of synchronizing the underlying persistent store with persistable state held in memory.)


9 thoughts on “Hibernate–从Session的save()方法看持久态、缓存

  1. Hi! I’ve beewn following youur wesblog ffor a whil nnow annd fially gott tthe braery too ggo aheaad annd givfe yyou a shout outt froim New Caney Texas!
    Juust wannted too saay keep upp thee ecellent work!

  2. Hello! This post could not be written any better! Reading through
    this post reminds me of my good old room mate! He always kept talking about this.
    I will forward this page to him. Pretty sure he will have a good read.
    Thanks for sharing!

  3. This is the perfect website for everyone who
    wants to find out about this topic. You know a whole lot
    its almost tough to argue with you (not that I actually would want to…HaHa).
    You definitely put a fresh spin on a subject that’s been written about for many
    years. Great stuff, just excellent!

  4. Thank you for such a wonderful blog. Where else could anyone get that kind of info written in such a perfect way?

    I have a presentation that I am presently writing on, and I have been on the look out for such excellent information.
    Pleased to discover your site.


电子邮件地址不会被公开。 必填项已用*标注