What is the difference between getHibernateTemplate().update and getHibernateTemplate().merge

hibernate, java, spring

Solution

Merge can work across sessions whereas update works only for the same session.

Although both are used for converting the object which is into its detached state to convert the object into its persisted state, but update will work only if we are in the same session, but merge will work even if its different session.

For more info.

By open session, it means that you are currently performing the operations in the same session (i.e. before closing the session)

Problem

I am working on web project in which i want to update user into the database. While writing code I found that it was giving an error if used as getHibernateTemplate().update(user) that "Illegal attempt to associate a collection with two open sessions;" But if I tried the same using `getHibernateTemplate().merge(user);` it perfectly worked fine. What is the difference between those two ? And what exactly means by open session ?

Original source