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
Suicide Squad ISEKAI: Neues Charakter-Video stellt Peacemaker vor
wallpaper-1019588
Wistoria: Zauberstab & Schwert” – Neues Visual veröffentlicht
wallpaper-1019588
Wie teuer ist ein Urlaub in Kroatien?
wallpaper-1019588
Die Hafenstadt Kotor in Montenegro