Assume I have the following database table structure:
A >- B -< C >- D
where >- is a many-to-one relation and -< one-to-many.
Now I would like to delete all A entries which correspond to an D with a given name.
One might say that
DELETE FROM A JOIN B ON <condition> JOIN C ON <condition> JOIN D ON <condition> WHERE D.name=?
would be a solution. Sadly, it appears that you cannot have a JOIN
clause in a DELETE
query.
Another approach was to have a sub-query, like this:
DELETE FROM A AS elem WHERE elem.id IN ( SELECT id FROM A JOIN B ON <condition> JOIN C ON <condition> JOIN D ON <condition> WHERE D.name=?);
While this might work in Oracle, MySQL will not let me do this(I cannot make a select on the table I'm about to delete from).
So how should I accomplish this?
To be more precise, I am using Hibernate and it's HQL to create this query. So a JPA/Hibernate solution would be preferred.
Of course, simple SQL will also do(I will try to translate it into HQL).