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