Take the 2-minute tour ×

Merge will update Name to Manik or Ankit ?

Student s1 = null;
s1 = (Student)session1.get(Student.class, new Integer(101));
Session session2 = factory.openSession();
Student s2 = null;
Object o1 = session2.get(Student.class, new Integer(101));
s2 = (Student)o1;
Transaction tx=session2.beginTransaction();
share|improve this question
add comment

3 Answers

It should update the name to "Manik" (FYI the OP's original Q: Persistence context cache the id and SQL query?) .

Hibernates weird mutable object cache (which IMHO I have always felt is stupid idea storing mutable objects in a cache) is discussed here: http://apmblog.compuware.com/2009/02/16/understanding-caching-in-hibernate-part-one-the-session-cache/

Merge should replace s2 with s1 reattaching s1 to the context/session. It will save it to the db once you flush or close the session. If you had saved s2 and then merged s1 I think but not certain that you might get an Opportunistic Lock exception on save particularly if you share the session across threads.

The best way to know for sure is to write a unit test.

share|improve this answer
add comment

this will update with name Manik.We will use merge to update the values with out considering the session state.

share|improve this answer
add comment

This will update to Manik because of this : Use merge() if you want to merge your modifications at any time without consideration of the state of the session. from hibernate documentation.

share|improve this answer
add comment

Your Answer


