Hibernate Session merge, update, save, saveOrUpdate, persist example

Hibernate Session is the interface between java application and hibernate framework. Today we will look into Session important methods for saving and updating data in tables – save, saveOrUpdate, persist, update and merge.

Hibernate Session

Hibernate Session save

As the method name suggests, hibernate save() can be used to save entity to database. We can invoke this method outside a transaction, that’s why I don’t like this method to save data. If we use this without transaction and we have cascading between entities, then only the primary entity gets saved unless we flush the session.

For our testing purposes we have two entity beans – Employee and Address.

Here is a simple hibernate program where we are invoking save() method in different cases.

When we execute above program, it produces following output.

Few important points that we can confirm from above output are:

  • We should avoid save outside transaction boundary, otherwise mapped entities will not be saved causing data inconsistency. It’s very normal to forget flushing the session because it doesn’t throw any exception or warnings.
  • Hibernate save method returns the generated id immediately, this is possible because primary object is saved as soon as save method is invoked.
  • If there are other objects mapped from the primary object, they gets saved at the time of committing transaction or when we flush the session.
  • For objects that are in persistent state, save updates the data through update query. Notice that it happens when transaction is committed. If there are no changes in the object, there wont be any query fired. If you will run above program multiple times, you will notice that update queries are not fired next time because there is no change in the column values.
  • Hibernate save load entity object to persistent context, if you will update the object properties after the save call but before the transaction is committed, it will be saved into database.

Hibernate Persist

Hibernate persist is similar to save (with transaction) and it adds the entity object to the persistent context, so any further changes are tracked. If the object properties are changed before the transaction is committed or session is flushed, it will also be saved into database.

Second difference is that we can use persist() method only within the boundary of a transaction, so it’s safe and takes care of any cascaded objects.

Finally, persist doesn’t return anything so we need to use the persisted object to get the generated identifier value. Let’s look at hibernate persist with a simple program.

Output produced by above code is:

Notice that first employee object is inserted, then at the time of transaction commit, update query is executed to update the name value. Also mapped object address is saved into database.

Hibernate saveOrUpdate

Hibernate saveOrUpdate results into insert or update queries based on the provided data. If the data is present in the database, update query is executed.

We can use saveOrUpdate() without transaction also, but again you will face the issues with mapped objects not getting saved if session is not flushed.

Hibernate saveOrUpdate adds the entity object to persistent context and track any further changes. Any further changes are saved at the time of committing transaction, like persist.

Above program produces following output.

Notice that without transaction, only Employee gets saved and address information is lost.

With transaction employee object is tracked for any changes, thats why in last call there is no update in Employee table even though the value was changed in between, final value remains same.

Hibernate update

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. Let’s check this behavior with a simple program.

When we execute above program for the first time, we get following output.

On further execution, we get following output.

Notice that there are no updates fired after first execution because there are no update in values. Also notice the employee name is “Final updated name” that we set after invoking update() method. This confirms that hibernate was tracking the object for any changes and at the time of committing transaction, this value got saved.

Hibernate Merge

Hibernate merge can be used to update existing values, however this method create a copy from the passed entity object and return it. The returned object is part of persistent context and tracked for any changes, passed object is not tracked. This is the major difference with merge() from all other methods. Let’s look at this with a simple program.

Output in first execution is:

In further execution, output produced is:

Notice that the entity object returned by merge() is different from the passed entity. Also notice that in further execution, name is “Kumar”, this is because the returned object is tracked for any changes.

That’s all for Hibernate Session save and update methods, I hope that examples above will help you in clarifying any doubts you have.

By admin

Leave a Reply

%d bloggers like this: