-->
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.  [ 5 posts ] 
Author Message
 Post subject: CascadeType.MERGE
PostPosted: Tue Mar 01, 2005 11:21 am 
Senior
Senior

Joined: Tue Mar 02, 2004 6:17 pm
Posts: 151
Hi,

I've read both the Hibernate and EJB3 documentation on CascadeType.MERGE.

I understand the concept of cascading a merge() operation but I am not clear on what exactly this means in the following case:

class Dog
{
/**
* Each dog goes after a specific bone. Many dogs may go after the same bone
*/
@ManyToOne(cascade={CascadeType.CREATE, CascadeType.MERGE})
Bone getBone();
}

Now say in the above case, if we set a Bone associated with Dog and persist the Dog, then the Bone object will get persisted too right (because of CascadeType.CREATE)? I understand that...

Now when is CascadeType.MERGE actually used? If the Bone associated with a dog changes, does MERGE imply that the original Bone is removed from the DB? Or maybe does it imply that if a Dog's Bone is modified, then that change is persisted to the DB? Or does it imply anything else?

Please clarify when CascadeType.MERGE should be used through some small use-case. (It would be nice if the Hibernate documentation included an example too)

Thank you,
Gili


Top
 Profile  
 
 Post subject: Re: CascadeType.MERGE
PostPosted: Tue Mar 01, 2005 11:40 am 
Senior
Senior

Joined: Tue Mar 02, 2004 6:17 pm
Posts: 151
I think I better clarify *exactly* what I'm trying to do...

Image
\-> ManyToOne -> ImageSpecification

ImageSpecification
\-> height
\-> width


So basically many images might have the same image specification and if I create a new image with an image specification that doesn't already exist, I want it created. Fine so far. Now, if you modify the ImageSpecification associated with the Image it shouldn't modify the underlying ImageSpecification table. The original entry should be left alone (other Images might be using it) and a new ImageSpecification should be pointed to (created if necessary).

The final requirement I have is: ImageSpecifications should be removed (automatically?) if no Image refers to them. Sorta like garbage collection.

Now, I'm not sure that you could do all of this in Hibernate but I would appreciate your advice on it. My original thoughts is that ImageSpecification should be declared with:

@ManyToOne(cascade={CascadeType.CREATE})

Notice there is no cascade on MERGE. And then I guess I'd have to manually scan the ImageSpecification table on a nightly basis and delete any unused entries. Is this the best I can do?

Thanks,
Gili


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 01, 2005 1:04 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
if bone is modified and dog is merged

Code:
dog.getBone().setSize(4);
session.merge(dog);


then the bone representation in DB will be updated thanks to the merge cascading.

Don't cascade MERGE to the image specification

Code:
dog.setBone(null);
session.merge(dog);

Will not remove bone since it's a many to one

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 01, 2005 1:25 pm 
Senior
Senior

Joined: Tue Mar 02, 2004 6:17 pm
Posts: 151
emmanuel wrote:
if bone is modified and dog is merged

Code:
dog.getBone().setSize(4);
session.merge(dog);


then the bone representation in DB will be updated thanks to the merge cascading.

Don't cascade MERGE to the image specification

Wow, that clears up a lot. Thanks!

Code:
dog.setBone(null);
session.merge(dog);

Will not remove bone since it's a many to one


Hmm, in the case of a many-to-one relationship, will it *ever* remove bone? I mean, is Hibernate smart enough to remove the bone if no other dog refers to it or is it up for me to do manually?

Thank you,
Gili


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 01, 2005 1:33 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
No, you have to do that manually.


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