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条)

报告相同问题?

悬赏问题

  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误