总结我到目前为止通过不同的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.
对于具有指定标识符的实体:
save():立即返回实体的标识符。由于在调用save之前已将标识符分配给实体,因此不会立即触发insert。它在会话刷新时被触发。
persist():与save相同。它也会在冲洗时触发插入。
请验证理解是否正确。
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? http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html/objectstate.html
答案 0 :(得分:0)
save vs persist - 应在Transaction
中调用所有方法保存返回ID但不保留。我们不能坚持分离的对象,因为我们可以保存分离的对象。
update vs merge - 应在Transaction中调用所有方法。
当我有分离对象并且我有一个相同主键的持久性对象时,如果我想要保留分离的对象,那么: - 一个。如果我们在分离对象上使用update,它将通过异常。 湾如果我们在分离的对象上使用merge,它会将字段的值复制到持久性对象并在数据库中更新。
如果我们没有分离对象的同一主键的持久对象,那么我们可以使用update方法使其持久化。
应始终使用更好的合并方法。
get vs load
get方法总是在调用数据库时命中数据库,其中load返回一个代理对象。当我们访问对象的字段时,它将访问数据库。如果我们不需要使用对象字段的数据,我们只需要将该对象传递给另一个对象,然后我们应该使用load而不是get方法。