ErikEngerd wrote:
Now, what I want to have is that after saving the object [using merge()], the primary key and version of the object passed in by the client are also set. Nevertheless, merge() does not do this (as documented) and creates only a new persistent object in the session.
Now what I want to do is to update the primary keys and versions of the original object passed in by the client. Is there a simple way to do this in Hibernate?
I see two simple options: either set the key and version yourself (i.e. in your application code) based on the Object returned by merge(), or extend Hibernate's DefaultMergeEventListener to do this for you and register this subclass as the merge event handler.
If you wrap all access to Session.merge() in your own method, the first option shouldn't be too hard to implement and might be a bit more obvious for other readers of the code. The second solution prevents others from circumventing your own merge-implementation and using Session.merge() directly without setting the key and version.
Joris