If in Hibernate, I do the following steps:
My question is what would be saved to the database - the name before I did session.save() or the name after the change i.e. "Other Name"?
Difference between save and saveOrUpdate in Hibernate The main difference between save and saveOrUpdate method is that save() generates a new identifier and INSERT record into the database while saveOrUpdate can either INSERT or UPDATE based upon the existence of a record.
The save() method INSERTs an object in the database. It will persist the given transient instance, first assigning a generated identifier. It returns the id of the entity created. The saveOrUpdate() calls either save() or update() on the basis of identifier exists or not.
The save method proves to be of less use in a long-running conversation that has extended a given Session context. As the persist method is called outside the transaction boundaries, it is utilized in long-running conversations that offer an extended Session context. Save() method gets support only through Hibernate.
No. Strictly speaking, Hibernate does not send an SQL update on update . update simply updates the object in the current session. Hibernate executes queries when the session is flushed.
The moment you saved the entity, it becomes managed and all further changes are propagated to the database during Session.flush
.
When you saved the entity, you only triggered an EntityInsertAction
to be queued. After you changed the entity, the current entity state was changed so during a flush, Hibernate will simply insert the latest entity state, so the database will contain the "Other Name"
.
In fact, calling a method like save
for a managed entity (which triggers an entity merge
) is actually going to affect performance.
While working with managed entity, when you don't call save, it will be saved automatically.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With