Java Hibernate 抛出无法删除或更新父行:外键约束失败
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32146911/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Hibernate throws Cannot delete or update a parent row: a foreign key constraint fails
提问by learner
I am working on a basic example to test cascade delete
operation but I am getting exception.
我正在编写一个基本示例来测试cascade delete
操作,但出现异常。
I have below entities:
我有以下实体:
Employee.java
雇员.java
@Entity
public class Employee {
@Id
@Column(name = "EMP_ID")
private long id;
private String name;
@OneToMany(mappedBy = "employee")
@Cascade(value = { CascadeType.REMOVE, CascadeType.SAVE_UPDATE })
private List<EmpDetails> details = new ArrayList<EmpDetails>();
}
EmpDetails.java
EmpDetails.java
@Entity
public class EmpDetails {
@Id
private long id;
private int info;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "EMP_ID")
private Employee employee;
}
Now I have records in databse with employee id as 10 and corresponding records in employee details table.
现在我在数据库中有记录,员工 ID 为 10,员工详细信息表中有相应的记录。
Now when I run below query:
现在,当我运行以下查询时:
session.beginTransaction();
session.delete(new Employee(10)); // here 10 is the ID of the employee
session.getTransaction().commit();
session.close();
I was thinking hibernate will delete the employee record and the corresponding employee details records as I have set the cascade type to remove. But I am getting exception as :
我在想 hibernate 会删除员工记录和相应的员工详细信息记录,因为我已将级联类型设置为删除。但我得到了例外:
Caused by: com.mysql.jdbc.exceptions.MySQLIntegrityConstraintViolationException: Cannot delete or update a parent row: a foreign key constraint fails
引起:com.mysql.jdbc.exceptions.MySQLIntegrityConstraintViolationException:无法删除或更新父行:外键约束失败
Can someone please help me how to test the cascade delete option here?
有人可以帮我在这里测试级联删除选项吗?
采纳答案by JB Nizet
The REMOVE cascade type is for the standard JPA remove()
operation. For the native Hibernate delete()
operation, you need to use a Hibernate-proprietary annotation:
REMOVE 级联类型用于标准 JPAremove()
操作。对于原生 Hibernatedelete()
操作,您需要使用Hibernate-proprietary annotation:
@Cascade(CascadeType.DELETE)
回答by besartm
When you delete Employee on session try to add this, I had the same issue:
当您在会话中删除 Employee 尝试添加此内容时,我遇到了同样的问题:
session.delete(session.get(Employee.class, employee_Id));
On my issue I had Movie and TimeTable relation was OneToOne:
在我的问题上,我有电影和时间表的关系是一对一:
On Movie model:
在电影模型上:
public class Movie implements Serializable
{
@Id
@Column(name = "fid")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int fid;
....
@OneToOne(mappedBy = "movie", cascade = CascadeType.ALL, orphanRemoval = true)
private TimeTable timetable;
}
On TimeTable model:
时间表模型:
public class TimeTable implements Serializable
{
...
@OneToOne
@JoinColumn(name = "fid")
private Movie movie;
}