아무거나

Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot delete or update a parent row: a foreign key constraint fails 에러 본문

Java/JPA

Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot delete or update a parent row: a foreign key constraint fails 에러

전봉근 2018. 9. 12. 16:25
반응형

@OneToOne이나 @OneToMany .. 등의 외래키 관계의 데이터를 삭제할 경우에 Foreign key가 지정되어있는 경우


Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot delete or update a parent row: a foreign key constraint fails  ...... FOREIGN KEY ....


라는 에러를 출력한다.


해결방법은 만약 entity의 annotation이 @OneToMany이라고 하면 해당 annotation에 cascade 설정을 추가한다.

@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinColumn(name = "brand_id")
private List<BrandProduct> brandProduct;

반응형
Comments