-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Session.merge(), detached objects, versions and primary keys
PostPosted: Fri Oct 28, 2005 6:10 pm 
Newbie

Joined: Thu Jan 20, 2005 12:21 pm
Posts: 9
Hi,


In my application I have a service that works with persistent objects. Nevertheless, I don't want to give clients of the service direct access to these objects since that would allow them to bypass the service and make changes to persistent objects directly (at least if the session is still open). Therefore, whenever I return a persistent object from my service, I create a copy of it.

Now, in order to save an object passed by the client to the service, I use session.merge() to save the object. This ensures that after the operation, a persistent object is present in the session. Calling saveOrUpdate() is not an option since (1) the object might already be in the session (I don't know) and (2) the object passed in from the client should remain detached.

Now, what I want to have is that after saving the object, 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?

Cheers
Erik


Top
 Profile  
 
 Post subject: Re: Session.merge(), detached objects, versions and primary
PostPosted: Sat Oct 29, 2005 10:42 am 
Beginner
Beginner

Joined: Thu Jun 02, 2005 5:09 am
Posts: 22
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


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 02, 2005 8:34 am 
Newbie

Joined: Thu Jan 20, 2005 12:21 pm
Posts: 9
Quote:
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.


I have already implemented the first operation. The only problem with it is that it is not generic because it requires every persistent class to implement an interface that I defined (which is ok for my application but which is not generic).

Extension of the DefaultMergeEventListener might also be an option if I want to use a more generic mechanism. Could the functionality that I describe be part of the standard hibernate APIs perhaps? Perhaps through an additional method session.merge(Object aObject, boolean updateOriginal) where updateOrignal would update the object passed in with the obtained primary key and (optional) version.

Cheers
Erik


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.