首页 新闻 会员 周边

菜鸟求助!!

0
悬赏园豆:20 [待解决问题]

最后的几个园豆了,各位帮帮忙吧
/
删除部门
*/
public String delete() throws Exception {
MessageBean messageBean = null;
try {

        Dept de = new Dept();
        de.setDeptId(deptId);
        de.setDeptName(deptService.getDept(de.getDeptId()).getDeptName());
        if(de.getChildren().size()!=0){
            for(Dept dept:de.getChildren()){
                
                deptService.deleteDept(dept);
                
            }
        }   
        
        deptService.deleteDept(de);
        //添加删除操作记录
        journalService.addOperationJournal(request, "部门删除", "删除内容:"+de);
        
        
        
        map.put("success", true);
    } catch (Exception e) {
        e.printStackTrace();
        map.put("success", false);
        map.put("message", e.getMessage());
    }

    GsonUtil.writeToResponse(map, false);
    return NONE;
}

我要在删除里面添加操作记录,但是这里重新将对象初始化了,如果我用之前的就会报错,求助各位大佬,这应该怎么写,初始化的对象里面只有id,但是如果我用这个id调用service中的通过id查询查询的方法就会报错
//保存部门名称
de.setDeptName(deptService.getDept(de.getDeptId()).getDeptName());

这是报的错
Caused by: org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [com.gyes.system.dept.bean.Dept#02]
at org.hibernate.engine.StatefulPersistenceContext.checkUniqueness(StatefulPersistenceContext.java:638)
at org.hibernate.event.def.DefaultDeleteEventListener.onDelete(DefaultDeleteEventListener.java:120)
at org.hibernate.event.def.DefaultDeleteEventListener.onDelete(DefaultDeleteEventListener.java:73)
at org.hibernate.impl.SessionImpl.fireDelete(SessionImpl.java:956)
at org.hibernate.impl.SessionImpl.delete(SessionImpl.java:934)
at org.springframework.orm.hibernate3.HibernateTemplate$25.doInHibernate(HibernateTemplate.java:843)
at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:406)
... 120 more

ppplum的主页 ppplum | 初学一级 | 园豆:3
提问于:2019-06-20 12:38
< >
分享
所有回答(1)
0

好久没用hibernate了,你参考这个帖子:
http://fatkun.com/2011/04/org-hibernate-nonuniqueobjectexception.html
就是让你使用Dept的时候别new,用load读出来用

小光 | 园豆:1766 (小虾三级) | 2019-06-20 14:01
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册