Hibernate 级联删除失败问题

要求:通过删除一个部门,级联删除它的所有下级部门
部门实体:Department.java

import java.util.HashSet;
import java.util.Set;
/**
 * 部门管理
 * @author Administrator
 *
 */
public class Department {

    private Long id;//部门id
    private String name;//部门名称
    private String description;//部门描述
    private Set<User> users = new HashSet<User>();//用户
    private Department parent;//上级部门
    private Set<Department> children = new HashSet<Department>();//下级部门

    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getDescription() {
        return description;
    }
    public void setDescription(String description) {
        this.description = description;
    }
    public Department getParent() {
        return parent;
    }
    public void setParent(Department parent) {
        this.parent = parent;
    }
    public Set<Department> getChildren() {
        return children;
    }
    public void setChildren(Set<Department> children) {
        this.children = children;
    }
    public Set<User> getUsers() {
        return users;
    }
    public void setUsers(Set<User> users) {
        this.users = users;
    }   
}

部门映射文件Departmetn.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="com.sinosoft.test.domain">

    <class name="Department" table="test_department">
        <id name="id">
            <generator class="native"/>
        </id>
        <property name="name"></property>
        <property name="description"></property>

        <!-- users属性,本类与User的一对多关系 -->
        <set name="users">
            <key column="departmentId"></key>
            <one-to-many class="User"/>
        </set>
        <!-- parent属性,本类与Department(上级)的多对一关系 -->
        <many-to-one name="parent" class="Department" column="parentId"></many-to-one>

        <!-- children属性,本类与Department(下级)的一对多关系
        cascade = "delete" 级联删除子部门,即当删除某部门连同删除其所有子部门
         -->
        <set name="children" cascade="all-delete-orphan">
            <key column="parentId"></key>
            <one-to-many class="Department"/>
        </set>              
    </class>

</hibernate-mapping>

DepartmentAction.java

package com.sinosoft.test.view.action;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.sinosoft.test.domain.Department;
import com.sinosoft.test.service.DepartmentService;

@Controller
@Scope("prototype")
public class DepartmentAction extends ActionSupport implements ModelDriven<Department> {

    @Resource
    private DepartmentService departmentService;
    private Long parentId;
    private Department model = new Department();

    public Department getModel() {
        return model;
    }

    public void setModel(Department model) {
        this.model = model;
    }

    /**部门列表*/
    public String list() throws Exception{
        List<Department> departmentList = departmentService.findAll();
        ActionContext.getContext().put("departmentList", departmentList);
        return "list";

    }

    /**添加页面*/
    public String addUI() throws Exception{
        //准备数据
        List<Department> departmentList = departmentService.findAll();
        ActionContext.getContext().put("departmentList", departmentList);
        return "addUI";
    }

    /**添加*/
    public String add() throws Exception{
        //封装对象到表单中
        Department parent = departmentService.getById(parentId);
        model.setParent(parent);
        departmentService.save(model);
        return "toList";
    }

    /**修改页面*/
    public String editUI() throws Exception{
        //准备数据departmentList
        List<Department> departmentList = departmentService.findAll();
        ActionContext.getContext().put("departmentList", departmentList);
        //准备回显数据
        Department department = departmentService.getById(model.getId());
        ActionContext.getContext().getValueStack().push(department);
        if(department.getParent() != null){
            parentId = department.getParent().getId();
        }

        return "editUI";
    }

    /**修改*/
    public String edit() throws Exception{
        //从数据库中取出原对象
        Department department = departmentService.getById(model.getId());
        //设置要修改的属性
        department.setName(model.getName());
        department.setDescription(model.getDescription());
        department.setParent(departmentService.getById(parentId));
        departmentService.update(department);
        return "toList";
    }

    /**删除*/
    public String delete() throws Exception{
        if(model.getId() != null){
        Department department = departmentService.getById(model.getId());
        departmentService.delete(department);
        }
        return "toList";
    }
    //
    public Long getParentId() {
        return parentId;
    }

    public void setParentId(Long parentId) {
        this.parentId = parentId;
    }


}

在我第一次操作时,没有级联成功,只删除了部门记录,没有级联删除该部门的所有下级部门的记录。最后找到原因是我在DepartmentAction.java中的delete()操作错误。
第一次操作代码:

/**删除*/
    public String delete() throws Exception{
        departmentService.delete(model);
        }
        return "toList";
    }

修改后:

/**删除*/
    public String delete() throws Exception{
        if(model.getId() != null){
        Department department = departmentService.getById(model.getId());
        departmentService.delete(department);
        }
        return "toList";
    }

错误原因是,因为我第一次删除的Model中只有页面传过来本部门的id,没有要删除的子部门信息,故不能删除,所以需要通过id先把该对象加载进来如下:

Department department = departmentService.getById(model.getId());

其它情况待补充。。。。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值