java.sql.SQLIntegrityConstraintViolationException: Cannot delete or update a parent row: a foreign key constraint fails (sns.comment, CONSTRAINT FKs1slvnkuemjsq2kj4h3vhx7i1 FOREIGN KEY (post_id) REFERENCES post (id))
post
와 comment
엔티티는 연관관계로 매핑되어 있다.post
는 hard delete로 구현, comment
는 soft delete로 구현post
를 delete
하려고 하자 위와 같은 에러가 발생comment
는 post
를 참조하고 있기 때문에 post
가 완전 삭제 되면 무결성 위반으로 위와 같은 에러가 발생comment
와 post
둘다 soft delete로 구현한다.
Comment
@Where(clause = "deleted_at IS NULL")
@SQLDelete(sql = "UPDATE comment SET deleted_at = CURRENT_TIMESTAMP where id = ?")
public class Comment extends BaseEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
Post
@Where(clause = "deleted_at IS NULL")
@SQLDelete(sql = "UPDATE post SET deleted_at = CURRENT_TIMESTAMP where id = ?")
public class Post extends BaseEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;