日期:2014-05-20  浏览次数:20822 次

一对多级联删除
[code=Java]
Commontop.xml
      <set   name= "pouringgirder "   lazy= "false "   cascade= "all ">
            <key>
                  <column   name= "ct_id "   not-null= "false "> </column>
            </key>
            <one-to-many   class= "com.zjrd.entity.Pouringgirder "/>
      </set>

Pouringgirder.xml
      <many-to-one   name= "commontop "   class= "com.zjrd.entity.Commontop "     fetch= "select "   not-found= "ignore "   insert= "false "   update= "false ">
              <column   name= "ct_id "   not-null= "true "   > </column>
      </many-to-one>

delete.action
public   String   deleteCommontop(){
int   id=Integer.parseInt(request.getParameter( "id "));
this.cmtBiz.deleteCommontop(id);
return   "delete ";
}
[/code]
关联id保存在多的一方Pouringgirder表中,并且是非空。然后运行,直接报错:
严重:   Servlet.service()   for   servlet   default   threw   exception
com.microsoft.sqlserver.jdbc.SQLServerException:   不能将值   NULL   插入列   'ct_id ',表   'Bridge.dbo.pouringgirder ';列不允许有空值。UPDATE   失败。
at   com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:196)
at   com.microsoft.sqlserver.jdbc.SQLServerStatement.getNextResult(SQLServerStatement.java:1454)
                  ...
由报错信息,我觉得是删除操作并不是在删除一表的时候直接删除关联多表,而是把多表的关联id改为null了,关联多表还是存在的,然后我想要的是在删除一表的时候,直接删除与之相关联的多表。
上网搜了一遍后,只找到了一个答案,在一表配置文件的 <set> 内加入inverse= "true ",交出主控权,再次运行之后,功能实现了,但是再次报错:
严重:   Servlet.service()   for   servlet   default   threw   exception
java.lang.NumberFormatException:   null
at   java.lang.Integer.parseInt(Integer.java:415)
at   java.lang.Integer.parseInt(Integer.java:497)
at   com.zjrd.action.CommontopAction.findCommontopById(CommontopAction.java:58)
at   sun.reflect.NativeMethodAccessorImpl.invoke0(Native   Method)
                  ...
对此,十分费解,思路完全串不起来,求高手解惑,或者指点下正确的功能实现方法。
想要的结果:级联删除,删除一,直接关联删除多!

------解决方案--------------------
第一个异常:
删除Commontop时,Commontop对应的Pouringgirder所关联的Commontop就不存在了。
而Commontop要维护关系,所以就修改外键为空。而外键被设置为not nul.
我的异常网推荐解决方案:Servlet.service() for servlet default threw exception,http://www.myexception.cn/eclipse/181756.html