{"id":10667,"date":"2018-07-31T05:05:14","date_gmt":"2018-07-31T03:05:14","guid":{"rendered":"http:\/\/blog.wenzlaff.de\/?p=10667"},"modified":"2020-06-24T19:50:24","modified_gmt":"2020-06-24T17:50:24","slug":"hibernate-cannot-delete-or-update-a-parent-row-a-foreign-key-constraint-fails","status":"publish","type":"post","link":"http:\/\/blog.wenzlaff.de\/?p=10667","title":{"rendered":"Hibernate: Cannot delete or update a parent row: a foreign key constraint fails"},"content":{"rendered":"<p>Es kommt beim l\u00f6schen eines BEs mit Hibernate zu folgenden Error: <\/p>\n<p>&#8222;<strong>Cannot delete or update a parent row: a foreign key constraint fails<\/strong>&#8222;.<\/p>\n<p>Im BE gibt es mehrere OneToMany Verbindungen wie z.B. diese <\/p>\n<p><a href=\"http:\/\/blog.wenzlaff.de\/wp-content\/uploads\/2016\/06\/fix.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/blog.wenzlaff.de\/wp-content\/uploads\/2016\/06\/fix.png\" alt=\"\" width=\"3508\" height=\"2480\" class=\"aligncenter size-full wp-image-7141\" srcset=\"http:\/\/blog.wenzlaff.de\/wp-content\/uploads\/2016\/06\/fix.png 3508w, http:\/\/blog.wenzlaff.de\/wp-content\/uploads\/2016\/06\/fix-300x212.png 300w, http:\/\/blog.wenzlaff.de\/wp-content\/uploads\/2016\/06\/fix-768x543.png 768w, http:\/\/blog.wenzlaff.de\/wp-content\/uploads\/2016\/06\/fix-1024x724.png 1024w\" sizes=\"auto, (max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><\/a><br \/>\n<!--more--><\/p>\n<pre class=\"lang:java decode:true \" >\r\n\r\n@OneToMany(mappedBy = \"arzt\", \r\nfetch = FetchType.LAZY, \r\ncascade = { CascadeType.PERSIST, CascadeType.MERGE})\r\nprivate Set&lt;Fachbereich&gt; fachbereiche = new HashSet&lt;&gt;();\r\n\r\n<\/pre>\n<p>Wie kann nun ein kaskadierendes L\u00f6schen des BEs bewirkt werden?<\/p>\n<p>Ein <strong>CascadeType.REMOVE<\/strong> in der Aufz\u00e4hlung erg\u00e4nzen, so:<\/p>\n<pre class=\"lang:java decode:true \" >\r\n\r\n@OneToMany(mappedBy = \"arzt\", \r\nfetch = FetchType.LAZY, \r\ncascade = { CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REMOVE})\r\nprivate Set&lt;Fachbereich&gt; fachbereiche = new HashSet&lt;&gt;();\r\n\r\n<\/pre>\n<p>Das l\u00f6st zwar das Problem mit der Exception, ist aber nicht so performant wie der Artikel &#8222;<a href=\"https:\/\/thorben-janssen.com\/avoid-cascadetype-delete-many-assocations\/\" rel=\"noopener noreferrer\" target=\"_blank\">Why you should avoid CascadeType.REMOVE for to-many associations and what to do instead<\/a>&#8220; zeigt. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Es kommt beim l\u00f6schen eines BEs mit Hibernate zu folgenden Error: &#8222;Cannot delete or update a parent row: a foreign key constraint fails&#8222;. Im BE gibt es mehrere OneToMany Verbindungen wie z.B. diese<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[220,5,79,3327,3326],"tags":[3359,2057,3358,2532,2150,1869,3357],"class_list":["post-10667","post","type-post","status-publish","format-standard","hentry","category-anleitung","category-java","category-programmierung","category-spring-programmierung","category-vaadin","tag-cannot-delete-or-update-a-parent-row-a-foreign-key-constraint-fails","tag-delete","tag-exception","tag-fix","tag-hibernate","tag-loeschen","tag-onetomany"],"_links":{"self":[{"href":"http:\/\/blog.wenzlaff.de\/index.php?rest_route=\/wp\/v2\/posts\/10667","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/blog.wenzlaff.de\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blog.wenzlaff.de\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blog.wenzlaff.de\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/blog.wenzlaff.de\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10667"}],"version-history":[{"count":0,"href":"http:\/\/blog.wenzlaff.de\/index.php?rest_route=\/wp\/v2\/posts\/10667\/revisions"}],"wp:attachment":[{"href":"http:\/\/blog.wenzlaff.de\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10667"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.wenzlaff.de\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10667"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.wenzlaff.de\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10667"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}