hibernate envers:merge& saveOrUpdate [英] hibernate envers: merge & saveOrUpdate
问题描述
我正在研究spring-hibernate-envers应用程序。很多谷歌搜索的东西终于为我工作,但我仍然有几个问题。
-
之前我为
储蓄使用saveOrUpdate
更新实体。但是在使用envers时
是
抛出nonUniqueObject
异常。所以我使用了merge
而不是
,它工作。为此使用
合并是否正确?是否合并
插入
个新对象到db?
-
我试过以下代码:
- 如果对象没有标识符属性,则save()它 >
- 如果对象的标识符具有赋值给新实例化对象的值,则save()它如果对象由or或版本化,则
- value是分配给新实例化对象的相同值save()它否则update()对象
和merge()是非常不同的: b
$ b- 如果存在当前与会话相关联的具有相同标识符的持久实例,如果当前没有与会话关联的持久实例,则将给定对象的状态复制到持久实例
- 上,尝试加载它从数据库,或创建一个新的持久实例
- 持久实例返回
- 给定的实例不会与会话相关联,它仍保持分离
Earlier i was using
saveOrUpdate
for saving or updating entities. But when working with envers it was throwing anonUniqueObject
exception. So i usedmerge
instead and it worked. Is it right to use merge for this? Doesmerge
inserts new objects to db?I tried following code:
- 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
- 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
entity = merge(entity);
saveOrUpdate(entity);
这也有效。这是正确的方式吗?而且我很好奇,为什么 saveOrUpdate
现在不会抛出任何错误。 Hibernate Reference 说:
$ b
saveOrUpdate()执行以下操作: 如果对象在此会话中已持久存在,则不做任何操作 li> 如果与该会话关联的另一个对象具有相同的标识符,则抛出一个异常
这意味着您可以使用 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.
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:
and merge() is very different:
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屋!