Unable to cascade delete a @OneToOne member
Question
@Entity public class Organization {
@OneToOne(fetch = FetchType.EAGER)
@OnDelete(action = OnDeleteAction.CASCADE)
@Cascade(value = DELETE_ORPHAN)
private Days days;
}
I have the following entity definition and it generates a SQL to do a cascade delete on the @OneToOne entry when the parent object gets deleted. But it's not removing the "days" entry while deleting an Organization.
This happens with h2, mysql databases, what could be the problem here.
Solution
My query looks like this "delete from Organization where some_key_id = ?" (am not deleting this based on primary key id)
A bulk delete (you should mention that in your question) doesn't cascade to anything. Quoting the JPA 1.0 specification:
4.10 Bulk Update and Delete Operations
...
A delete operation only applies to entities of the specified class and its subclasses. It does not cascade to related entities.
This is a very annoying limitation and there are many RFEs to improve things (HHH-695, HHH-1917, HHH-3337, HHH-5529, etc).
For now, possible solutions include:
- clean up the child table yourself
- use cascading foreign keys in the schema.
Now the weird part... My understanding of @OnDelete(action = OnDeleteAction.CASCADE)
is that this annotation is supposed to be used to ensure that the foreign key is created with the appropriate ON DELETE CASCADE
clause (solution #2). In other words, I'd expect things to work.
Did Hibernate generate the Organization
table? Can you check the DDL? Do you see the expected ON DELETE CASCADE
? If not, add it.
OTHER TIPS
Well, I guess you should add a
@Cascade(value = {DELETE, DELETE_ORPHAN})
Note that in JPA 2.0 you don't have to use the hibernate-sepcific @Cascade
annotation - @*ToMany
has an option to delete orphans.
Update: when using queries cascades are not handled. You have to handle them manually. This is expected and documented behaviour.