njnareshjoshi/articles

Getting Referential integrity constraint violation exception when trying to delete parent (file) entity

Opened this issue · 0 comments

Hello Naresh - thanks for the post, which is of great assistance.

Currently, using Spring boot 2.3 with jpa. I tried to write my own poc with Book and BookHistory entity (corresponding to File and FileHistory), with other audit * classes same. The BookHistory is manyToOne with Book.

The add/insert fine are logging correctly in BookHistory table, it is just that when I try to delete a 'Book', the operation is failing as hibernate seems to be complaining because it is not willing to leave the record "orphaned" in BookHistory (when a Book is deleted). By any chance you faced similar issue?

This is exception trace
org.h2.jdbc.JdbcSQLIntegrityConstraintViolationException: Referential integrity constraint violation: "FKABA5IOAFYJ1YQA0TBC27TV1EB: PUBLIC.BOOK_HISTORY FOREIGN KEY(BOOK_ID) REFERENCES PUBLIC.BOOK(BOOK_ID) (1)"; SQL statement:
delete from book where book_id=? [23503-200]
at org.h2.message.DbException.getJdbcSQLException(DbException.java:459) ~[h2-1.4.200.jar:1.4.200]
at org.h2.message.DbException.getJdbcSQLException(DbException.java:429) ~[h2-1.4.200.jar:1.4.200]