If I modify the Hibernate object, after performing the save, when I commit, the changes will be saved to the database

If in sleep mode I do the following steps:

  • Open a session.
  • Create a new POJO hibernation object (which must be saved) and fill in the values.
  • Now I am doing session.save ().
  • Now I am modifying the POJO object - let's say that this is an employee object, and I am doing emp.setName ("Other Name"). Change the employee name in the object.
  • Now I am executing a transaction.

My question is what will be stored in the database - name before I started session.save () or name after changing ie "Other name"?

+6
source share
3 answers

the moment you save the object , it becomes manageable, and all further changes are applied to the database during the Session Cleanup .

When you save an object, you only EntityInsertAction . After you changed the object, the current state of the object was changed, therefore, during a flash, Hibernate simply inserts the last state of the entity, therefore the database will contain "Other Name" .

In fact, calling a save method on a managed object (which runs the merge object) actually affects performance. Leave this post for more details .

+9
source

All you do in the session is commit to the database as soon as you complete the transaction. Even when you clear a session, you simply add the changes to the table to memory, but do not commit the database.

So, as soon as Hibernate detects that the session is dirty, it will make a change if you have automatic dirt checking in your configuration and it will be smart enough to see the last change in your object session. So yes, you will have a "Other Name"

+1
source

When working with a managed object, when you do not call a save, it will be automatically saved.

+1
source

Source: https://habr.com/ru/post/989445/


All Articles