-->
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.  [ 2 posts ] 
Author Message
 Post subject: Deleting objs belonging to other collections (cascade or not
PostPosted: Wed Aug 22, 2007 5:44 pm 
Beginner
Beginner

Joined: Tue Nov 29, 2005 4:42 pm
Posts: 49
Location: Atlanta, GA
I have 3 objects with relationships between each other. I'm deleting 2 of them, and I need the many-to-many relationship cleaned up. I want to simply remove it from the collection rather than deleting the owning object of the relationship. Here is my classes and relationships:
Code:
class Policy {

   @OneToMany( mappedBy="policy", cascade = CascadeType.REMOVE )
   List<PolicyVersion> policyVersions;

}

class PolicyVersion {

   @ManyToOne
   Policy policy;

}

class Job {

   @ManyToMany // owning side of the ManyToMany
   List<PolicyVersion> policyVersions;

}



In this case I want to remove a Policy, and it needs to delete all of the PolicyVersion it owns. The above mapping will do just that. If I delete a Policy then all of the policy versions are deleted.

However, if I have a Job that references a PolicyVersion then if I try and delete the Policy object it fails with a constraint violation because PolicyVersion ID is stuck in a join table.

I tried to add a bi-directional reference in PolicyVersion to Job and set the relationship to CascadeType.REMOVE, but it doesn't every work. It's as if it ignores it. This new relationship is not the owning side of the relationship.

Can I cascade from the non-owning side of a relationship? Will the CascadeType.REMOVE actually remove it from the collection rather than deleting the Job? I want it just to remove the entry in the join table between Job and PolicyVersion (i.e. Job_PolicyVersion). I tried not setting the cascade and it still doesn't work.

If I have to use a delete statement (i.e. query) how to I say remove this thing from the collection and leave the Job instance?

Any ideas?

Charile


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 23, 2007 11:39 am 
Beginner
Beginner

Joined: Tue Nov 29, 2005 4:42 pm
Posts: 49
Location: Atlanta, GA
Well once again I figure it out on my own. But, I always like to contribute back to the forum in case someone else runs into this.

The answer is yes you can cascade both sides of the relationship. It doesn't matter that one side owns the relationship or not. The trick is I needed to do a refresh to associate the object with the session BEFORE deleting. So the answer was as simple as:

Code:
session.refresh( policy );
session.remove( policy );


Why did refresh fix my problem? Well in my unit tests I wasn't maintaining the relationship between PolicyVersion and DeploymentJob in my code. Since I'm only mapping this relationship to cleanup the dependancies it doesn't have to maintained. But, this came back to bite me because if the relationship is null or empty hibernate thinks there's no DeploymentJob's with a relationship back to PolicyVersion. Hence nothing to delete.

Refresh initializes the PolicyVersion.jobs relationship and so Hibernate sees it as a delete.

Charlie


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