在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的目的没有体现