hibernate envers:merge& saveOrUpdate [英] hibernate envers: merge & saveOrUpdate

查看:142
本文介绍了hibernate envers:merge& saveOrUpdate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究spring-hibernate-envers应用程序。很多谷歌搜索的东西终于为我工作,但我仍然有几个问题。


  1. 之前我为
    储蓄使用 saveOrUpdate 更新实体。但是在使用envers时

    抛出 nonUniqueObject
    异常。所以我使用了 merge 而不是
    ,它工作。为此使用
    合并是否正确?是否合并插入
    个新对象到db?

  2. 我试过以下代码:


  3.   entity = merge(entity); 
    saveOrUpdate(entity);


    这也有效。这是正确的方式吗?而且我很好奇,为什么 saveOrUpdate 现在不会抛出任何错误。 Hibernate Reference 说:
    $ b


    saveOrUpdate()执行以下操作:
    如果对象在此会话中已持久存在,则不做任何操作 li> 如果与该会话关联的另一个对象具有相同的标识符,则抛出一个异常
  4. 如果对象没有标识符属性,则save()它
  5. >
  6. 如果对象的标识符具有赋值给新实例化对象的值,则save()它如果对象由or或版本化,则
  7. value是分配给新实例化对象的相同值save()它否则update()对象


    和merge()是非常不同的: b
    $ b


    • 如果存在当前与会话相关联的具有相同标识符的持久实例,如果当前没有与会话关联的持久实例,则将给定对象的状态复制到持久实例

    • 上,尝试加载它从数据库,或创建一个新的持久实例

    • 持久实例返回

    • 给定的实例不会与会话相关联,它仍保持分离


  8. 这意味着您可以使用 saveOrUpdate() code>如果您确定具有相同标识符的对象不与会话关联。否则,您应该使用 merge()



    以下代码

      entity = merge(entity); 
    saveOrUpdate(entity);

    可以工作,因为 merge()是一个持久对象,因此被 saveOrUpdate()忽略,所以第二行没有任何意义。


    I am working on an spring-hibernate-envers application. After lot of googling things are finally working for me but i have still got couple of questions.

    1. Earlier i was using saveOrUpdate for saving or updating entities. But when working with envers it was throwing a nonUniqueObject exception. So i used merge instead and it worked. Is it right to use merge for this? Does merge inserts new objects to db?

    2. I tried following code:

    entity=merge(entity);  
    saveOrUpdate(entity);
    

    This also worked. Is it the right way? And also i am curious that why saveOrUpdate is not throwing any error now.

    解决方案

    Hibernate Reference says:

    saveOrUpdate() does the following:

    • if the object is already persistent in this session, do nothing
    • if another object associated with the session has the same identifier, throw an exception
    • if the object has no identifier property, save() it
    • if the object's identifier has the value assigned to a newly instantiated object, save() it
    • if the object is versioned by a or , and the version property value is the same value assigned to a newly instantiated object, save() it
    • otherwise update() the object

    and merge() is very different:

    • if there is a persistent instance with the same identifier currently associated with the session, copy the state of the given object onto the persistent instance
    • if there is no persistent instance currently associated with the session, try to load it from the database, or create a new persistent instance
    • the persistent instance is returned
    • the given instance does not become associated with the session, it remains detached

    It means that you can use saveOrUpdate() if you are sure that the object with the same identifier is not associated with the session. Otherwise you should use merge().

    The following code

    entity=merge(entity);
    saveOrUpdate(entity); 
    

    works because the result of merge() is a persistent object, therefore it's ignored by saveOrUpdate(), so that the second line doesn't make any sense.

    这篇关于hibernate envers:merge& saveOrUpdate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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