在hibernate3中使用批量删除,删除带父子关系表的数据时删除不了。具体代码如下:
   a表级联b表(a表是父表)
    <set
        name="multilanguages"
        lazy="true"
        inverse="true"
cascade="all-delete-orphan"
    >
        <key>
            <column name="info_Id" />
        </key>
        <one-to-many
            class="com.Multilanguage"
        />
    </set>    执行的语句如下:
       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)
请问是哪里设置有问题?另外,我用不带关联的单表进行批量删除的时候,就能删除成功     

解决方案 »

  1.   

    inverse="false" 
    改了试一下
      

  2.   

    。晕 好莫名其妙的错。"Cannot delete or update a parent row: a foreign key constraint fails" 外键SQL Error: 1217, SQLState: 23000 
    生成的sql报错。
      

  3.   

    inverse="true" cascade="all"   把lazy="true" 去掉 配置没什么问题  
    仔细检查下你代码看有什么错误没有
      

  4.   

    检查过了,换了一个单表的就能删除。我现在是删除主表,在删除主表的时候,附表也会一起删除。
    inverse="false"试过了一样的问题