关联关系n-to-n错误

关联关系n-to-n错误

员工和部门是多对一的关系

在修改部门的时候,会出现n-to-n错误,就是咱们修改的时候也在相应的修改它的部门(这时候部门是一个持久化对象,它的id是不允许进行修改的。)

解决方案:(在获到员工的时候把部门设置为空)


/**
 * 特性:在执行相应方法之前都会先执行这个方法
 */
@ModelAttribute("editEmployee")
public Employee beforeEdit(Long id, String cmd){
    //有id的时候-> 修改功能
    if(id!=null && "update".equals(cmd)) {
        Employee employee = employeeService.findOne(id);
        //把这个要修改的关联对象设置为null,可以解决n-to-n的问题
        employee.setDepartment(null);
        return employee;
    }
    return null;
}

猜你喜欢

转载自blog.csdn.net/Secutiry_/article/details/88940957
N!
n
N*