问题描述
我正在研究spring-hibernate-envers应用程序。很多谷歌搜索的东西终于为我工作,但我仍然有几个问题。
-
之前我为
储蓄使用saveOrUpdate
更新实体。但是在使用envers时
是
抛出nonUniqueObject
异常。所以我使用了merge
而不是
,它工作。为此使用
合并是否正确?是否合并
插入
个新对象到db?
-
我试过以下代码:
Earlier i was using
saveOrUpdate
forsaving or updating entities. Butwhen working with envers it wasthrowing anonUniqueObject
exception. So i usedmerge
insteadand it worked. Is it right to usemerge for this? Doesmerge
insertsnew objects to db?I tried following code:
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.
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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!