我有一个数据库,其中所有的表都是由 Java JPA/Hibernate 代码生成的。我需要以与此类似的方式更新数据库中的几个表,
UPDATE Department SET id=100000 WHERE id=0;
不幸的是,这会导致错误
错误 1451 (23000):无法删除或更新父行:外键约束失败(departmentuserlink,CONSTRAINT FK96AF44EAB09C41C5 FOREIGN KEY (department_id) REFERENCES department (id))
以下是 Java 实体:
@Entity
@Table(name="Department")
public class Department extends AbstractEntity implements IAbstractEntity {
@OneToMany(cascade=CascadeType.ALL, mappedBy="department", fetch=FetchType.EAGER)
private Set<DepartmentJobLink> departmentJobs = new HashSet<DepartmentJobLink>(0);
//Setters & getters and all that
}
@Entity
@Table(name="edrDepartmentJobLink", uniqueConstraints={@UniqueConstraint(columnNames={"department_id", "job_id"})})
public class DepartmentJobLink extends AbstractEntity implements IAbstractEntity {
@ManyToOne(fetch=FetchType.EAGER)
@JoinColumn(name="department_id", nullable=false)
private Department department;
//Setters & getters and all that
}
他们在超类中都有 ID。到目前为止,它们的其他一切看起来都很好,只是在更新 Department 的主键时遇到了麻烦。我会很感激任何建议。谢谢。