简体   繁体   English

Hibernate save vs saveOrUpdate vs update vs persist

[英]Hibernate save vs saveOrUpdate vs update vs persist

Summarizing what I discovered so far through different stackoverflow posts: 总结我到目前为止通过不同的stackoverflow帖子发现的内容:

Persist
New
a) out of transaction: allowed. persist accomplished the task of saving both Parent and Child in one call.
b) in of transaction: allowed.
detached
a) out of transaction: allowed. will throw exception when trying to flush.
b) in of transaction: not allowed. will throw exception when trying to flush.
existing
a) out of transaction: allowed. will throw exception when trying to flush.
b) in of transaction: not allowed. will throw exception when trying to flush.

saveOrUpdate
new
a) out of transaction: allowed. will save.
b) in or transaction: allowed. will save.
detached
a) out of transaction: allowed. will update.
b) in of transaction: allowed. will update.
existing
a) out of transaction: allowed. will update.
b) in of transaction: allowed. will update.

save
new
a) out of transaction: allowed. will save. Not sure how can it be saved right away without transaction. save does not cascade to the child, i.e., only Parent is saved/inserted in the table.
b) in or transaction: allowed. will save.
detached
a) out of transaction: save() for a detached object will create a new row in the table.
b) in or transaction: not allowed. will throw exception.
existing
a) out of transaction: not sure what will happen.
b) in or transaction: not allowed. will throw exception.

update
new
a) out of transaction: not sure what will happen. should throw exception.
b) in or transaction: not sure what will happen. should throw exception.
detached
a) out of transaction: allowed. will update later during flush.
b) in or transaction: allowed. will update later during flush.
existing
a) out of transaction: allowed. will update later during flush.
b) in or transaction: allowed. will update later during flush.

merge
new
a) out of transaction: not sure what will happen.
b) in or transaction: if there is no persistent instance currently associated with the session, try to load it from the database, or create a new persistent instance.
detached
a) out of transaction: allowed. will update later during flush. return a copy.
b) in or transaction: allowed. will update later during flush. return a copy.
existing
a) out of transaction: allowed. will update later during flush. return a copy.
b) in or transaction: allowed. will update later during flush. return a copy.

For Entities with assigned identifier : 对于具有指定标识符的实体:

save(): It returns an entity's identifier immediately. save():它立即返回实体的标识符。 Since the identifier is already assigned to entity before calling save, so insert is not fired immediately. 由于在调用save之前已将标识符分配给实体,因此不会立即触发insert。 It is fired at session flush time. 它在会话刷新时被触发。

persist() : same as save. persist():与save相同。 It also fire insert at flush time. 它还在冲洗时触发插入。

Please verify whether understanding is correct. 请验证理解是否正确。

Hibernate persist() vs save() method Hibernate saveOrUpdate vs update vs save/persist Hibernate persist vs save What's the advantage of persist() vs save() in Hibernate? Hibernate persist()vs save()方法 Hibernate saveOrUpdate vs update vs save / persist Hibernate persist vs save Hibernate 中persist()vs save()的优点是什么? http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html/objectstate.html http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html/objectstate.html

save vs persist -- all methods should be called within Transaction save vs persist - 应在Transaction中调用所有方法

save return id but persist not. 保存返回ID但不坚持。 we can not persist detached object where as we can save detached object. 我们不能坚持分离的对象,因为我们可以保存分离的对象。

update vs merge -- all methods should be called within Transaction. update vs merge - 应在Transaction中调用所有方法。

when I have detached object and i have a persistence object of the same primary key then if I want to persist the detached object then :- a. 当我有分离对象并且我有一个相同主键的持久性对象时,如果我想要保留分离的对象,那么: - a。 If we use update on the detached object, it will through exception. 如果我们在分离对象上使用update,它将通过异常。 b. If we use merge on the detached object, it will copy the values of fields to the persistence object and update in database. 如果我们在分离的对象上使用merge,它会将字段的值复制到持久性对象并在数据库中更新。

If we don't have the persistence object of the same primary key of detached object, then we can use update method to make it persistence. 如果我们没有分离对象的相同主键的持久性对象,那么我们可以使用update方法使其持久化。

Better merge method should be used always. 应始终使用更好的合并方法。

get vs load 得到vs负载

get method always hit the database when it is invoked, where as load returns a proxy object. get方法在调用时始终命中数据库,其中load返回代理对象。 When we access the field of the object, then it will hit the database. 当我们访问对象的字段时,它将访问数据库。 If we don't need to use the data of field of object, we only need that object to pass to another object then we should use the load instead of get method. 如果我们不需要使用对象字段的数据,我们只需要将该对象传递给另一个对象,然后我们应该使用load而不是get方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM