The nuances of the merge() method

If you want to use Hibernate to change an object that has already been stored in the database, then there are also several methods for this.

The first is the merge() method , which updates the information in the database based on the passed object . This will invoke the SQL UPDATE query. Example:

User user = new User();
user.setName("Kolyan");
session.save(user);

session.evict(user);     // detach the object from the session
user.setName("Masha");

User user2 = (User) session.merge(user);

There are several important nuances here.

First, the merge() method returns the result, the updated object. This object has a Persist state and is attached to the session object. The object passed to the merge() method does not change.

It may seem that there is no difference between user and user2, but it is not. You can pass a POJO object to the merge() method , and as a result, the method can return a proxy (depending on the Hibernate settings). So just remember that the merge() method does not change the passed object.

Secondly, if the object passed to merge() has the Transient status (and it does not have an ID), then a separate line will be created for it in the database. In other words, the persist() command will be executed .

Thirdly, if an object already attached to the session (with the Persist status) is passed to the merge() method, then nothing will happen - the method will simply return the same object. Why? And all because when the transaction is committed, the data will be written to the database anyway:

User user = new User();
user.setName("Kolyan");
session.save(user);

user.setName("Masha"); //change the object attached to the session

session.close();  //all changed objects will be written to the database

There is no need to save the object every time after any of its changes. If this object is in Persist status, then Hibernate will do everything itself. If you change an object that is “attached to the base”, then all its changes will be written to the base.

The nuances of the update() method

Hibernate also has an update() method , which, like the save() method , was inherited from previous versions. With this method, you can only update the data of an already saved object. This will invoke the SQL UPDATE query. Example:

User user = new User();
user.setName("Kolyan");
session.save(user);

session.evict(user);     // detach the object from the session
user.setName("Masha");

session.update(user);

This method does not return anything and does not change the existing object.

If you call this method on a new object, then an exception will simply be thrown:

User user = new User();
user.setName("Kolyan");
session.update(user);   //an exception will be thrown here

saveOrUpdate() Method

Before the advent of JPA, the function of the persist() method was performed by the saveOrUpdate() method . His task was to update the information on the existing object in the database, and if there is none, then create it. It is almost always used in place of the save() and update() methods .

Unlike the update() method , it can change the object passed to it. For example, set it to the ID that was assigned when saving to the database. Example:

User user = new User();
user.setName("Kolyan");
session.saveOrUpdate(user);   //object will be written to the database

How it works:

  • if the passed object has an ID, then the UPDATE SQL method is called
  • if the ID of the passed object is not set, then the INSERT SQL method is called
undefined
1
Task
Module 4. Working with databases, level 11, lesson 2
Locked
task1102
task1102