cacade 删除操作在休眠中未按预期工作
我尝试按如下方式实现cascaded =“delete”,但只有员工行被删除,而不是相应的工资行。 Employee pojo 类是:
public class Employee {
int empid;
String ename;
Salary sal;
//gettrs and setters
工资 pojo 类是:
public class Salary {
int sid;
double netsal;
Employee employee;
//getters and setters
employee.hbm 文件是:
<hibernate-mapping>
<class name="employee.Employee" table="EMPLOYEE">
<id name="empid" type="int">
<column name="EMPID" />
<generator class="assigned" />
</id>
<property name="ename" type="java.lang.String">
<column name="ENAME" />
</property>
<many-to-one name="sal" class="employee.Salary" cascade="delete">
<column name="sid" />
</many-to-one>
</class>
salary.hbm 文件是:
<hibernate-mapping>
<class name="employee.Salary" table="SALARY">
<id name="sid" type="int">
<column name="sid" />
<generator class="increment" />
</id>
<property name="netsal" type="double">
<column name="netsal" />
</property>
</class>
相应的mysql表创建为:
create table employee(EMPID int(5) primary key,ENAME varchar(20),sid int(5),foreign key(sid) references salary(sid));
create table salary(sid int(5) primary key,netsal int(5));
插入记录后,例如:
SessionFactory sf= new Configuration().configure().buildSessionFactory();
session= sf.openSession();
Transaction tx= session.beginTransaction();
Employee emp=new Employee();
emp.setEmpid(12);
emp.setEname("some name");
Salary sal=new Salary();
sal.setNetsal(12000);
emp.setSal(sal);
sal.setEmployee(emp);
tx.commit();
session.flush();
session.close();
当我
Query query = session.createQuery("delete from employee e where e.empid=12");
query.executeUpdate();
这样做时,雇员表中的条目将被删除,但工资表中的相应条目仍然保留。那么我如何确保相应的工资条目也被删除? 谢谢。
i tried implementing cascaded="delete" as follows but only the employee row gets deleted and not the corresponding salary row.
The Employee pojo class is :
public class Employee {
int empid;
String ename;
Salary sal;
//gettrs and setters
The salary pojo class is :
public class Salary {
int sid;
double netsal;
Employee employee;
//getters and setters
the employee.hbm file is :
<hibernate-mapping>
<class name="employee.Employee" table="EMPLOYEE">
<id name="empid" type="int">
<column name="EMPID" />
<generator class="assigned" />
</id>
<property name="ename" type="java.lang.String">
<column name="ENAME" />
</property>
<many-to-one name="sal" class="employee.Salary" cascade="delete">
<column name="sid" />
</many-to-one>
</class>
the salary.hbm file is:
<hibernate-mapping>
<class name="employee.Salary" table="SALARY">
<id name="sid" type="int">
<column name="sid" />
<generator class="increment" />
</id>
<property name="netsal" type="double">
<column name="netsal" />
</property>
</class>
and the corresponding mysql tables are created as:
create table employee(EMPID int(5) primary key,ENAME varchar(20),sid int(5),foreign key(sid) references salary(sid));
create table salary(sid int(5) primary key,netsal int(5));
after inserting records like:
SessionFactory sf= new Configuration().configure().buildSessionFactory();
session= sf.openSession();
Transaction tx= session.beginTransaction();
Employee emp=new Employee();
emp.setEmpid(12);
emp.setEname("some name");
Salary sal=new Salary();
sal.setNetsal(12000);
emp.setSal(sal);
sal.setEmployee(emp);
tx.commit();
session.flush();
session.close();
when i do
Query query = session.createQuery("delete from employee e where e.empid=12");
query.executeUpdate();
then the entry in the employee table is deleted but the corresponding entry in salary table remains.So how do i ensure that the corresponding salary entry is also deleted?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 DML 样式删除查询删除实体时,级联不起作用。仅当您使用会话删除员工时它才会起作用:
The cascade doesn't work when deleting entities using DML-style delete queries. It will work only if you delete the employee using the session: