foreign key constraint를 위반한 엔티티를 insert하면 어떻게 될까?

jinwook han·2024년 4월 29일
0

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`))

0개의 댓글

관련 채용 정보