I think that merge is the proper way to reattach objects. The Hibernate API for Session.merge(obj) states the following

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”.

This seems to contradict your claim that it works if and only if an object exists in the hibernate session. According to the Javadoc the instance will be loaded if it is not currently associated with the Session.

 

Source:http://stackoverflow.com/questions/912659/what-is-the-proper-way-to-re-attach-detached-objects-in-hibernate

Advertisements