jQuery in Action, 3rd edition
The moose likes Object Relational Mapping and the fly likes Required information on session.merge() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Required information on session.merge()" Watch "Required information on session.merge()" New topic

Required information on session.merge()

Bhavesh Dak
Ranch Hand

Joined: Jun 27, 2009
Posts: 93
Hi all,

Can any body explain me what exactly the session.merge() does?

SCJP 6(83%), SCWCD 5.0(100%)
Ramasamy Anbalagan

Joined: Dec 14, 2009
Posts: 20
Hi Bhavesh,

When we doing reattachment process in hibernet, we used session.merge(). By doing this all changes are merged as single one then sync with database.
Bhavesh Dak
Ranch Hand

Joined: Jun 27, 2009
Posts: 93
Thanks Ramasamy,

Here is my scenario, I have to use same function twice one for saving an object with fresh data (data that is not in DB) and one for updating and object which will have data that is loaded from db and updated by application.

If i put only session.saveOrUpdate(object) I am getting NonUniqueObject Exception.

When I am calling session.merge(object) it is not updating data to db but executes without any exception.

So what should I need to do to make my function work for both saving and updating data.

Bhavesh Dak
Ranch Hand

Joined: Jun 27, 2009
Posts: 93
session.merge() behaves very strange as I don't know how to use it correctly.

I am able to resolve my issue by manipulating relational properties of Objects and with the help of inverse property of hibernate.

Mark E Hansen
Ranch Hand

Joined: Apr 01, 2009
Posts: 650
Merge() is used to re-connect an Entity with the persistence session.

As an example, assume you have an Entity class named User.

In your EJB, you create a new User:

User user = new User();

This object is not associated with the persistence unit yet.

You then set some values:

user.setFirstName("first name");

You then associate it with the persistence unit, using the persist() method:


Now, the persistence unit will manage the User object, saving it to the database when it is time (not necessarily right away).

Let's say you send this User object to the web client, so the web application can give it to the user to edit. This will disconnect it from the persistence unit. When the application sends the object back to the EJB, you need to re-associate the (currently) disconnected User object with the persistence unit. You do this using the context.merge(user) method.

When you call merge(), it will re-associate the object with the managed User object, and update the values to those passed in the user-edited User object.

At some later time, the persistence unit will take care of applying the changes to the database record.

So, persist() is used for associating new Entity objects with the persistence unit, and merge() is used for re-associating disconnected (but existing) Entity objects with the persistence unit.

By the way, this is all explained very well in the book: EJB3 in Action from Manning Publications.

Bhavesh Dak
Ranch Hand

Joined: Jun 27, 2009
Posts: 93
Thanks Mark E Hansen for information

I will surely do some practicals on it.

Right now I have found my way out to the problems but I am very much curious to use these fundamentals in my projects.

Thanks again ..
I agree. Here's the link: http://aspose.com/file-tools
subject: Required information on session.merge()
It's not a secret anymore!