Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Hibernate update an entity that has not changed?

Given an unchanged entity, does Hibernate session.update(entity) send an SQL UPDATE statement to the database server?

For example, in the following code, does Hibernate send an SQL UPDATE to the database server?

Session session = factory.openSession();
Person me = new Person(null, "Derek Mahar");
session.save(me);
Person me2 = new Person(me.getId(), "Derek Mahar");
session.update(me2);
session.flush();
like image 713
Derek Mahar Avatar asked Nov 27 '10 20:11

Derek Mahar


People also ask

How does update work in Hibernate?

Hibernate update should be used where we know that we are only updating the entity information. This operation adds the entity object to persistent context and further changes are tracked and saved when transaction is committed.

Does Hibernate update all fields?

Hibernate always updates all database columns mapped by my entity, even the ones that I didn't change.

What is the Hibernate life cycle of an entity?

In Hibernate, we can either create a new object of an entity and store it into the database, or we can fetch the existing data of an entity from the database. These entity is connected with the lifecycle and each object of entity passes through the various stages of the lifecycle.

What is difference between Merge and update in Hibernate?

A merge() method is used to update the database. It will also update the database if the object already exists. An update() method only saves the data in the database. If the object already exists, no update is performed.


1 Answers

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.

like image 91
Bozho Avatar answered Sep 21 '22 07:09

Bozho