Answer the question
In order to leave comments, you need to log in
How to delete an Entity and all associated Entities along with it?
Here is an example of Entity, extra methods and fields are omitted.
public class Restaurant extends AbstractNamedEntity {
@OneToMany(mappedBy = "restaurant", cascade = CascadeType.ALL, orphanRemoval = true)
private Set<Vote> votes;
@OneToMany(mappedBy = "restaurant", cascade = CascadeType.ALL, orphanRemoval = true)
private Set<Dish> dishes;
}
public static final String GRAPH_RESTAURANT_WITH_VOTES_DISHES = "Restaurant with votes-dishes.Graph";
@Transactional
@Modifying
@EntityGraph(GRAPH_RESTAURANT_WITH_VOTES_DISHES)
@Query("DELETE FROM Restaurant r WHERE r.id=:id")
int deleteRestaurantById(int id);
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question