-->
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.  [ 4 posts ] 
Author Message
 Post subject: re-attaching without cascading assocaitions...
PostPosted: Sun Oct 01, 2006 9:34 am 
Newbie

Joined: Sun Oct 01, 2006 8:46 am
Posts: 2
Hi,

I've got a situation where I have set up appropriate cascading on associations to create them without having to worry about saving those objects directly, essentially allowing hibernate to do its stuff. This works well and I'm happy with that when my business code is in charge of the creation. However, on receiving a dettached domain object from another application, I only want to update the top level entity itself, only its base properties not any underlying associations, to limit any stupidity from the client. The update essentially pertains only to the inbound entity.

Currently, in the update method on my service I just do a lookup of the object from the db and copy the properties from the inbound object in to the looked up version and then allow the changes to flush. This means I am in control of which props get updated:

Code:
public void update(Object obj) {
    Object toUdate = dao.get(obj.getId());
    //call the copy method to update only those props that are valid
    toUpdate.copy(obj);
    //implicitly save on transaction completion
}


However as I am not reattaching the object to the session, this removes the ability to use Hibernate's automatic versioning and optimistic locking that could be achieved with reattaching.

Question is; is there any way to change the cascading settings based on the reattachement to only flush changes to the top level object? i.e. I am happy with my cascade settings when creating objects during a session and when I am in control, but don't want those cascades to apply on reattaching the object to a session.

Apologies on the long post, can't cut it down much smaller :)

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Sun Oct 01, 2006 5:11 pm 
Regular
Regular

Joined: Tue Sep 26, 2006 11:37 am
Posts: 115
Location: Sacramento, CA
You may want to consider using inverse=true in the main object and let the many-to-one side of the relationship do the updates.

http://www.hibernate.org/hib_docs/v3/re ... ientupdate

I hope this helps :)

Marius


Top
 Profile  
 
 Post subject: Re: re-attaching without cascading assocaitions...
PostPosted: Mon Oct 02, 2006 3:28 am 
Beginner
Beginner

Joined: Sun Jan 22, 2006 6:56 am
Posts: 29
machibernate wrote:
However as I am not reattaching the object to the session, this removes the ability to use Hibernate's automatic versioning and optimistic locking that could be achieved with reattaching.


What do you mean? Does the detached object contain version data that you want to use?
As long as it doesn't you'll still have versioning and optimistic locking done by hibernate. The object's version was fetched when you loaded it, and it will be checked again when the session is flushed, to see that is wasn't changed in the meantime.

If you do want to use the detached object's version, you might consider using session.merge() or session.replicate(), but i'm not sure what's the purpose.


Top
 Profile  
 
 Post subject: being an idiot...
PostPosted: Mon Oct 02, 2006 5:35 am 
Newbie

Joined: Sun Oct 01, 2006 8:46 am
Posts: 2
My assumption, which I should have tested, was that if a client was to mess about with the object graph of a detached object and hand back to my service that calls saveOrUpdate on the detached object to reattach it to the session, hibernate would try to reflect all changes made to the object, including any new associations.

As I have mapped all associations with inverse=true, that is not the case, and hibernate does exactly what I require; which is to only update the inbound entity's properties and any many-to-one relationships with cascade=all set.

Job Done,

Thanks


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.