Hibernate: Cannot delete or update a parent row: a foreign key constraint fails

Es kommt beim löschen eines BEs mit Hibernate zu folgenden Error:

Cannot delete or update a parent row: a foreign key constraint fails„.

Im BE gibt es mehrere OneToMany Verbindungen wie z.B. diese

Hibernate: Cannot delete or update a parent row: a foreign key constraint fails

@OneToMany(mappedBy = "arzt", 
fetch = FetchType.LAZY, 
cascade = { CascadeType.PERSIST, CascadeType.MERGE})
private Set<Fachbereich> fachbereiche = new HashSet<>();

Wie kann nun ein kaskadierendes Löschen des BEs bewirkt werden?

Ein CascadeType.REMOVE in der Aufzählung ergänzen, so:

@OneToMany(mappedBy = "arzt", 
fetch = FetchType.LAZY, 
cascade = { CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REMOVE})
private Set<Fachbereich> fachbereiche = new HashSet<>();

Das löst zwar das Problem mit der Exception, ist aber nicht so performant wie der Artikel „Why you should avoid CascadeType.REMOVE for to-many associations and what to do instead“ zeigt.

Ähnliche Artikel:

  1. Many to Many mit Java Persistence API (JPA 2.0) und Spring Boot ohne XML
  2. One to One mit Java Persistence API (JPA 2.0) und Spring Boot ohne XML
  3. Wie kann Boilerplate Code in Java reduziert werden?

wallpaper-1019588
A Nobody’s Way Up to an Exploration Hero: Anime erscheint als Doppelfolge
wallpaper-1019588
[Manga] Nana & Kaoru [Max 9]
wallpaper-1019588
Reiseziele April
wallpaper-1019588
Dein Style-Guide: Finde deinen persönlichen Kleidungsstil