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
Die richtige Matratze für erholsamen Schlaf
wallpaper-1019588
Algarve News vom 27. Juli bis 02. August 2026
wallpaper-1019588
Liste von mehr als 190 italienischen Pasta Sorten
wallpaper-1019588
Hara Hachi Bu & Longevity: Warum 80% satt oft besser sind als 100%
wallpaper-1019588
Podcast „Algarve entdeckt“: Hubert Lechner über Immobilienbetrug, Neuanfang und die Kunst, Zeitmillionär zu sein