休眠:刷新,执行,复制和刷新 [英] Hibernate: Refresh, Evict, Replicate and Flush

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

问题描述

我希望我知道这个清单中的每件物品到底是什么,它是如何工作的,后果是什么以及何时是正确的使用时间。


  1. 刷新

  2. 退伍

  3. 复制

  4. 刷新
  5. / ol>

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



    我知道这是一个非常普遍的问题,但我认为真正有用了解这一​​切。



    谢谢。

    p>

    解决方案

    Hibernate文档 给出了很好的例子。此博客文章也会为您提供一些见解。我将在下面添加一行。



    可以随时使用刷新重新加载对象及其所有集合()方法。当数据库触发器用于初始化对象的某些属性时,这非常有用。

      sess.save(cat); 
    sess.flush(); //强制执行SQL INSERT
    sess.refresh(cat); //重新读取状态(执行触发器后)

    请参阅此处获取更多示例。



    无论何时您将对象传递给 save(),update()或saveOrUpdate(),并且每当使用 load() ,get(),list(),iterate()或scroll(),该对象被添加到Session的内部缓存中。 $ b

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

      ScrollableResult cats = sess.createQuery(from cat as cat) 。滚动(); //一个巨大的结果集
    while(cats.next()){
    Cat cat =(Cat)cats.get(0);
    doSomethingWithACat(cat);
    sess.evict(猫); //(如果给出编译时错误然后使用它:sess.evict(cat.getClass());
    }

    请阅读此处的完整示例



    阅读会话API 这里


    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. Refresh
    2. Evict
    3. Replicate
    4. Flush

    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.

    Thanks.

    解决方案

    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.

    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)
    

    see here for more examples.

    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.

    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());  
    }
    

    Read the complete example from here.

    Read about the session API here.

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

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