First CascadeType.Remove is not an option because the client side database may not support cascade operation.
I guess OrphanRemoval = true only handle @OneToXXX relationships.
How about many-to-one relationships?
For example a many-to-one mapping like this:
public class Order{
....
@ManyToOne
private Customer customer;
.....
}
In Customer entity there is no mapping to Order which make the relationship become one-direction.
Then how can we remove all orders from a customer before removing that customer? Is there a simple automatic way to handle that or we have to implement the code explicitly via service layer and call something like findOrdersByCustomer(long customer_id) then remove them?
Thanks in advance.
Yes, you’ll need to find all the orders and remove them. Or you can also execute a JPQL query deleting everything:
Note that your note about CascadeType.REMOVE is wrong: it’s JPA that executes the cascade, and not the database. It’s thus supported whatever the database is.