hql没有删除查询确定

Aad*_*dam 0 java hibernate hql spring-mvc

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);
}
Run Code Online (Sandbox Code Playgroud)

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

ete*_*nay 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();
Run Code Online (Sandbox Code Playgroud)