休眠:刷新、驱逐、复制和刷新 [英] Hibernate: Refresh, Evict, Replicate and Flush

查看:21
本文介绍了休眠:刷新、驱逐、复制和刷新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我知道这个列表中的每一项到底是做什么的,它是如何工作的,后果是什么,什么时候是正确的使用时间.

I wish I knew what exactly does each item in this list, how it works, what the consequences and when is the correct time to use.

  1. 刷新
  2. 驱逐
  3. 复制
  4. 冲洗

我什至想知道每个人做了什么,但我不确定,所以我请求你的帮助,因为我真的很想了解它.

I even wonder what each one does, but I'm not absolutely sure, so I'm asking for your help, cause I really want to understand it.

我知道这是一个非常笼统的问题,但我认为了解这一切真的很有用.

I know it's a pretty generic question, but I think really useful to know about it all.

谢谢.

推荐答案

Hibernate 文档 给出了很好的例子.此外,这篇博文 将为您提供一些见解.我将从下面添加一些行.

The Hibernate Documentation gives good examples of this. Also this blog post will give you some insight. I will add some line from there below.

使用refresh() 方法可以随时重新加载对象及其所有集合.这在使用数据库触发器初始化对象的某些属性时很有用.

It is possible to re-load an object and all its collections at any time, using the refresh() method. This is useful when database triggers are used to initialize some of the properties of the object.

sess.save(cat);
sess.flush(); //force the SQL INSERT
sess.refresh(cat); //re-read the state (after the trigger executes)

请参阅此处了解更多示例.

每当您将对象传递给 save()、update() 或 saveOrUpdate() 时,以及每当您使用 load()、get()、list() 检索对象时,iterate() 或 scroll(),该对象被添加到 Session 的内部缓存中.

Whenever you pass an object to save(), update() or saveOrUpdate(), and whenever you retrieve an object using load(), get(), list(), iterate() or scroll(), that object is added to the internal cache of the Session.

当随后调用 flush() 时,该对象的状态将与数据库同步.如果您不希望发生这种同步,或者您正在处理大量对象并需要有效地管理内存,则可以使用 evict() 方法将对象及其集合从一级缓存.

When flush() is subsequently called, the state of that object will be synchronized with the database. If you do not want this synchronization to occur, or if you are processing a huge number of objects and need to manage memory efficiently, the evict() method can be used to remove the object and its collections from the first-level cache.

ScrollableResult cats = sess.createQuery("from Cat as cat").scroll(); //a huge result set
while ( cats.next() ) {
    Cat cat = (Cat) cats.get(0);
    doSomethingWithACat(cat);
    sess.evict(cat);     //  (if gives the compile time error then use it: sess.evict(cat.getClass());  
}

此处阅读完整示例.

此处阅读有关会话 API 的信息.

Read about the session API here.

这篇关于休眠:刷新、驱逐、复制和刷新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆