本文介绍了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() 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 forsaving or updating entities. Butwhen working with envers it wasthrowing a nonUniqueObjectexception. So i used merge insteadand it worked. Is it right to usemerge for this? Does merge insertsnew objects to db?

    2. I tried following code:

    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:

    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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-25 10:36