lqh210000 2009-09-11 13:55
浏览 337
已采纳

hibernate3中批量删除问题(父子表删除)

在hibernate3中使用批量删除,删除带父子关系表的数据时删除不了。具体代码如下:
a表级联b表(a表是父表)
name="multilanguages"
lazy="true"
inverse="true"
cascade="all-delete-orphan"
>



class="com.Multilanguage"
/>

执行的语句如下: 
  StringBuffer query = new StringBuffer("delete Info as fi "); 
  调用bulkDelete(query); 

  public String bulkDelete(final String hsql) throws DAOException{ 

return (String)getHibernateTemplate().execute(new HibernateCallback(){
public Object doInHibernate(Session session)throws HibernateException{
int countnum=0;
try{
countnum=session.createQuery(hsql).executeUpdate();
} catch (HibernateException e) {
e.printStackTrace();

} catch (DataAccessException e) {
e.printStackTrace();
throw new DAOException(e);

} catch (Throwable e) {
e.printStackTrace();
throw new DAOException(e);

}
return String.valueOf(countnum);
}
},true);
}

    结果后台报错: 
        2009-09-11 12:33:14 WARN [org.hibernate.util.JDBCExceptionReporter] SQL Error: 1217, SQLState: 23000 

2009-09-11 12:33:14 ERROR [org.hibernate.util.JDBCExceptionReporter] Duplicate key or integrity constraint violation message from server: "Cannot delete or update a parent row: a foreign key constraint fails"
org.hibernate.exception.ConstraintViolationException: could not execute update query
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:71)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.hql.ast.exec.BasicExecutor.execute(BasicExecutor.java:84)
at org.hibernate.hql.ast.QueryTranslatorImpl.executeUpdate(QueryTranslatorImpl.java:334)
at org.hibernate.engine.query.HQLQueryPlan.performExecuteUpdate(HQLQueryPlan.java:209)
at org.hibernate.impl.SessionImpl.executeUpdate(SessionImpl.java:1126)
at org.hibernate.impl.QueryImpl.executeUpdate(QueryImpl.java:94)

请问是哪里设置有问题?

另外,我用不带关联的单表进行批量删除的时候,就能删除成功

[b]问题补充:[/b]
你这种做法我知道,这样要select多次,用hibernate的目的没有体现

  • 写回答

2条回答 默认 最新

  • iteye_18973 2009-09-12 16:44
    关注

    不用多次吧,只需要查一个根据id查父id对象,另一个是根据父id查询删除子集合

    因为我们有些都是逻辑删除的,所以都不配上级联删除或着更新,都是通过程序去写的!!!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料