hql没有删除查询确定

时间:2013-06-05 22:05:35

标签: java hibernate spring-mvc hql

sessionFactory.getCurrentSession().createQuery(
    "delete from Attendance attend where attend.id in(Select att.id from Attendance att where att.dat= :dat and att.cls_id= :cls_id)"
    ).setDate("dat", dat).setShort("cls_id",cls_id); // delete all records with matching date and cls_id
sessionFactory.getCurrentSession().flush();
sessionFactory.getCurrentSession().clear();
// immediate save after delete in same transaction
for(Attendance a : absent){
    sessionFactory.getCurrentSession().save(a);
}

我正在尝试删除具有特定条件的现有行,因为我保存的对象可能已存在于表中。但是删除不起作用,并且插入了重复值。请告诉我代码的哪一部分是错误的。

1 个答案:

答案 0 :(得分:5)

您没有执行查询,只是传递参数。您需要在查询中调用executeUpdate()方法:

sessionFactory.getCurrentSession().createQuery("delete from Attendance attend where attend.id in(Select att.id from Attendance att where att.dat= :dat and att.cls_id= :cls_id)")
                        .setDate("dat", dat).setShort("cls_id",cls_id).executeUpdate();