Java中用Hibernate框架:实现多对多关联删除问题!
实现方法:我也设置XML属性了!inverse="true"cascade="all"在删除时还是提示于数据库约束冲突//删除项目publicvoiddeleteProj...
实现方法:我也设置XML属性了! inverse="true" cascade="all"
在删除时还是提示于数据库约束冲突
//删除项目
public void deleteProject(){
Session session=HibernateSessionFactory.getSession();
Transaction tran=null;
try{
tran=session.beginTransaction();
Project project=(Project)session.load(Project.class, new Long(7));
Employee employee=(Employee)session.load(Employee.class, new Long(14));
Hibernate.initialize(project.getEmployees());//加载关联信息
project.getEmployees().remove(employee);
employee.getProjects().remove(project);
project.getEmployees().removeAll(project.getEmployees());
employee.getProjects().removeAll(employee.getProjects());
session.update(employee);
session.update(project);
session.delete(project);
tran.commit();
}catch(Exception ex){
tran.rollback();
ex.printStackTrace();
}finally{
session.close();
}
} 展开
在删除时还是提示于数据库约束冲突
//删除项目
public void deleteProject(){
Session session=HibernateSessionFactory.getSession();
Transaction tran=null;
try{
tran=session.beginTransaction();
Project project=(Project)session.load(Project.class, new Long(7));
Employee employee=(Employee)session.load(Employee.class, new Long(14));
Hibernate.initialize(project.getEmployees());//加载关联信息
project.getEmployees().remove(employee);
employee.getProjects().remove(project);
project.getEmployees().removeAll(project.getEmployees());
employee.getProjects().removeAll(employee.getProjects());
session.update(employee);
session.update(project);
session.delete(project);
tran.commit();
}catch(Exception ex){
tran.rollback();
ex.printStackTrace();
}finally{
session.close();
}
} 展开
1个回答
2013-11-16
展开全部
加到ONE-TO-MANY的一方!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询