1

Summarizing what I discovered so far through different stackoverflow posts:

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. Since the identifier is already assigned to entity before calling save, so insert is not fired immediately. It is fired at session flush time.

persist() : same as 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? http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html/objectstate.html

Community
  • 1
  • 1
user1539343
  • 1,569
  • 6
  • 28
  • 45

1 Answers1

0

save vs persist -- all methods should be called within Transaction

save return id but persist not. we can not persist detached object where as we can save detached object.

update vs merge -- all methods should be called within 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. If we use update on the detached object, it will through exception. b. If we use merge on the detached object, it will copy the values of fields to the persistence object and update in database.

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.

Better merge method should be used always.

get vs load

get method always hit the database when it is invoked, where as load returns a proxy object. 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.