Hibernate Merging and Refreshing Entities

In this hibernate tutorial, we will discuss the basics of and differences between refresh() and merge() methods present in hibernate Session interface.

  • At a very high level, refresh() means pulling any state changes from the database that have been done outside the current Session and after the entity has been loaded. Cascading the refresh() means that all associated entities are also refreshed.
  • The merge() means merging any state changes to the database that have been done to a detached entity which was previously a managed entity.

1. Refreshing Entities Using refresh()

Sometimes we face a situation when our application database is modified with some external application/agent and thus corresponding hibernate entity in your application actually becomes out of sync with its database representation i.e. having old data. In this case, we can use session.refresh() method to re-populate the entity with the latest data available in the database.

We can use the refresh() method to refresh an instance of a persistent object when database triggers are used to initialize some of the properties of the object.

These methods will reload the properties of the object from the database, overwriting them. In real-life applications, we do not have to use the refresh() method very often apart from the above-stated scenario.

session.flush(); //force the SQL INSERT and triggers

session.refresh(transaction); //re-read the state (after the trigger executes)

2. Merging Entities Using merge()

The method merge() does exactly the opposite of what refresh() does i.e. It causes the updates to the database with values from a detached entity. The refresh() method was updating the entity with the latest database information. So basically, both work almost the opposite.

Merging is performed when we desire to change a detached entity to the persistent state again, including the detached entity’s changes migrated to (or overriding) the database.

Employee employee = new Employee(); 


Employee mergedEmployee = (Employee) session.merge(employee);

This is very important to note that employee and mergedEmployee are two completely different objects, and we usually need to discard the employee object.

Hibernate official documentation give a very good explanation of merge() method: Copy the state of the given object onto the persistent object with the same identifier. If there is no persistent instance currently associated with the session, it will be loaded. Return the persistent instance. If the given instance is unsaved, save a copy of and return it as a newly persistent instance. The given instance does not become associated with the session. This operation cascades to associated instances if the association is mapped with cascade=”merge”.

That’s all for this tutorial covering merge() and refresh() methods in hibernate. Remember that question can be asked as the difference between merge() and refresh(), or difference between merge() and load() etc. Be ready to encounter any such difference between interview questions.

Happy Learning !!


Notify of
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.