Hibernate 级联删除未按预期工作
我正在使用 hibernate 3 并尝试删除数据库中的记录,但删除并未按我的预期进行。模式休眠正在工作(以伪代码):
create table Employer(
employer_id number(12) primary key,
employer_name varchar2(50)
);
create table Employee(
employee_id number(12) primary key,
employee_name varchar2(50),
employer_id number(12) foreign key references employer.employer_id not null
);
create table Employee_Roles(
role_id number(12) primary key,
employee_id number(12) foreign key references employee.employee_id not null,
role varchar2(50)
);
我的休眠类映射看起来像:
@Entity
public class Employer{
@Id
@Column(name = "EMPLOYER_ID")
private long id;
@Column
private String name;
@OneToMany(targetEntity = Employee.class, fetch = FetchType.EAGER)
@JoinColumn(name = "employer_id")
@Cascade(CascadeType.ALL)
private Set<Employee> employees;
}
@Entity
public class Employee{
@ManyToOne(targetEntity = Employer.class)
@JoinColumn(name = "employer_id")
@Cascade(value = CascadeType.SAVE_UPDATE)
private Employer employer;
@OneToMany(targetEntity = EmployeeRole.class, fetch = FetchType.EAGER)
@JoinColumn(name = "employee_id")
@Cascade(CascadeType.ALL)
private Set<Employee> employees;
}
@Entity
public class EmployeeRole{
@ManyToOne(targetEntity = Employee.class)
@JoinColumn(name = "employee_id")
@Cascade(value = CascadeType.SAVE_UPDATE)
private Employee employee;
}
现在使用此配置,我正在调用:
getCurrentSession().delete(someEmployerEntity);
发生的情况是:
Hibernate: update EMPLOYEE set EMPLOYEE_ID=null where EMPLOYEE_ID=?
Hibernate: update EMPLOYEE_ROLE set employee_id=null where employee_id==?
[2011-04-15 15:59:53,487] JDBCExceptionReporter WARN - SQL Error: -10, SQLState: 23502
[2011-04-15 15:59:53,487] JDBCExceptionReporter ERROR - integrity constraint violation: NOT NULL check constraint; SYS_CT_10058 table: EMPLOYEE_ROLE
并引发异常。我期望 session.remove(..) 调用的结果是要删除的雇主记录,以及与雇主关联的所有雇员记录以及与已删除的雇员记录关联的所有 EmployeeRole 记录。这是一个正确的假设吗?或者我在这里误解了一个关键概念?
I am using hibernate 3 and attempting to delete a record in the database, and the delete is not working as I would expect. The schema hibernate is working against (in pseudocode):
create table Employer(
employer_id number(12) primary key,
employer_name varchar2(50)
);
create table Employee(
employee_id number(12) primary key,
employee_name varchar2(50),
employer_id number(12) foreign key references employer.employer_id not null
);
create table Employee_Roles(
role_id number(12) primary key,
employee_id number(12) foreign key references employee.employee_id not null,
role varchar2(50)
);
My hibernate class mappings look something like:
@Entity
public class Employer{
@Id
@Column(name = "EMPLOYER_ID")
private long id;
@Column
private String name;
@OneToMany(targetEntity = Employee.class, fetch = FetchType.EAGER)
@JoinColumn(name = "employer_id")
@Cascade(CascadeType.ALL)
private Set<Employee> employees;
}
@Entity
public class Employee{
@ManyToOne(targetEntity = Employer.class)
@JoinColumn(name = "employer_id")
@Cascade(value = CascadeType.SAVE_UPDATE)
private Employer employer;
@OneToMany(targetEntity = EmployeeRole.class, fetch = FetchType.EAGER)
@JoinColumn(name = "employee_id")
@Cascade(CascadeType.ALL)
private Set<Employee> employees;
}
@Entity
public class EmployeeRole{
@ManyToOne(targetEntity = Employee.class)
@JoinColumn(name = "employee_id")
@Cascade(value = CascadeType.SAVE_UPDATE)
private Employee employee;
}
Now with this configuration I am calling:
getCurrentSession().delete(someEmployerEntity);
What is occurring is:
Hibernate: update EMPLOYEE set EMPLOYEE_ID=null where EMPLOYEE_ID=?
Hibernate: update EMPLOYEE_ROLE set employee_id=null where employee_id==?
[2011-04-15 15:59:53,487] JDBCExceptionReporter WARN - SQL Error: -10, SQLState: 23502
[2011-04-15 15:59:53,487] JDBCExceptionReporter ERROR - integrity constraint violation: NOT NULL check constraint; SYS_CT_10058 table: EMPLOYEE_ROLE
and an exception being raised. What I am expecting as a result of the session.remove(..) call is the employer record to be deleted, as well as all employee records associated with the employer and all EmployeeRole records associated with the deleted employee records. Is this a correct assumption? Or am I misunderstanding a key concept here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
级联所有删除孤儿应该可以解决您的问题。然而,它是 Hibernate 的一部分,而不是 EJB 标准。如果您想这样做并且不想陷入供应商的解决方案中,我建议您查看 这篇文章。
祝你好运!
编辑:根据您的建议,我将“mappedBy”属性添加到 @OneToMany 注释中,这似乎是使用 inverse="true" 来指定拥有关系的注释方式。关系的相关更改部分如下所示:
Cascade all-delete-orphan should solve your problem. However, it is part of Hibernate and not EJB standard. If you want to do it and do not be trapped in your vendors' solution, I would suggest you to have a look to this article.
Good luck!
EDIT: following your suggestions I added the 'mappedBy' attributes to the @OneToMany annotations which seems to be the annotations way of using inverse="true" for specifying the owning relationships. The relevant changed sections of the relationships look like: