foreign key constraint를 위반한 엔티티를 insert하면 어떻게 될까? -> java.sql.SQLIntegrityConstraintViolationException이 발생한다.
create table department (
dept_id BIGINT PRIMARY KEY,
upper_dept_id BIGINT,
dept_name VARCHAR(50),
FOREIGN KEY (upper_dept_id) REFERENCES department(dept_id)
}
@Entity
@Table(name = "department")
public class Department {
@Id
@Column(name = "dept_id")
private Long deptId;
@Column(name = "upper_dept_id")
private Long upperDeptId;
@Column(name = "dept_name")
private String deptName;
public Department() {
}
public Department(Long deptId, Long upperDeptId, String deptName) {
this.deptId = deptId;
this.upperDeptId = upperDeptId;
this.deptName = deptName;
}
}
Caused by: java.sql.SQLIntegrityConstraintViolationException:
Cannot add or update a child row:
a foreign key constraint fails
(`db`.`department`, CONSTRAINT `department_ibfk_1` FOREIGN KEY (`upper_dept_id`) REFERENCES `department` (`dept_id`))