deleted object would be re-saved的问题。

各位:

在多对多的关系中,我先修改一个关系,然后删除中间关联的数据,最后插入新的中间关联数据,此时报“deleted object would be re-saved by cascade......”的错误。

1、相关domain:
//权限表
class Purview {
       
    String purviewCode
   String purviewName

   static hasMany = [connRolePurview:ConnRolePurview]
       
}
//角色表
class Role {
       
   String  roleCode
  String  roleName
  String  roleDesc

  static hasMany = [connRolePurview:ConnRolePurview]
 
}
//权限角色关联表
class ConnRolePurview {
       
      Purview purview
    Role role
    String  operationType
 
    static belongsTo = [Purview,Role]
 
}

2、相关的controller的update操作:
class ConnRolePurviewController {
    ......
    def update = {
        def role = Role.get(params.id)
   
      Role.withTransaction{tx->
        try{
            //1.修改role.
           role.roleName = params.roleName
            role.roleDesc = params.roleDesc
            if(!role.save(flush:true)) {
                flash.message = "role's save error;"
                return redirect(action:edit,id:params.id)
            }
           sessionFactory.getCurrentSession().flush();
       
            //2.删除中间关联数据。
           def delRolePurview = ConnRolePurview.findAll("from ConnRolePurview crp where crp.role.id = "+params.id)
           if(delRolePurview)
               {
                for(objDel in delRolePurview)
                {
                    def delId = ConnRolePurview.get(objDel.id)
                    if(delId) {
                        delId.delete()
                         }
                }
               }

              //3.添加中间关联数据。        
            StringTokenizer st = new StringTokenizer(params.hidPurviews,"/")
              while (st.hasMoreTokens()) {
                StringTokenizer stPO = new StringTokenizer(st.nextToken(),"|")
                while (stPO.hasMoreTokens()) {
                    def purview = Purview.get(stPO.nextToken())
                    def connRolePurview = new ConnRolePurview()
                    connRolePurview.role = role
                    connRolePurview.purview = purview
                    connRolePurview.operationType = stPO.nextToken()
                                                 
                    }
               }
                               
                    return redirect(action:list)
                               
        }
        catch(Exception e){  
             tx.setRollbackOnly();
             flash.message = "the error for ("+e.toString()+")"
             return redirect(action:edit,id:params.id)
        }
               
        }
    }
}


报的错误如下:
[31766]StackTrace Sanitizing stacktrace:
org.springframework.dao.InvalidDataAccessApiUsageException:deleted object would be re-saved by cascade (remove deleted object from  associations): [ConnRolePurview#72];......

不知道这种情况如何处理?(就是先修改一个关系,然后删除中间数据,最后添加中间数据的情况。)

另外,不知道有没有其它的解决方法能够完成这样的需求,同时避免处理这种情况,例如:分步处理。(但是分步的话,对于界面操作会繁琐一些。)

你可能感兴趣的:(DAO,Flash)