Java hql未删除查询确定

Java hql未删除查询确定,java,hibernate,spring-mvc,hql,Java,Hibernate,Spring Mvc,Hql,我正在尝试删除具有特定条件的现有行,因为我正在保存的对象可能已经存在于表中。但该删除操作不起作用,并且正在插入重复的值。请告诉我代码的哪一部分是错误的。您不是在执行查询,只是在传递参数。您需要对查询调用executeUpdate()方法: sessionFactory.getCurrentSession().createQuery( "delete from Attendance attend where attend.id in(Select att.id from Attendanc

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

您不是在执行查询,只是在传递参数。您需要对查询调用
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); // 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);
}

您没有执行查询,只是传递参数。您需要对查询调用
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); // 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);
}

为什么这么复杂的查询,而不仅仅是
从attent att删除att.dat=:dat和att.cls_id=:cls_id
?我更改了它,在它不工作时变得复杂,这是我愚蠢的错误,可能是我太困了,忘记执行。抱歉,各位,非常感谢。为什么这么复杂的查询,而不仅仅是
从Attent att中删除att.dat=:dat和att.cls_id=:cls_id
?我更改了它,在它不工作时变得复杂,这是我愚蠢的错误,可能是我太困了,忘记执行。对不起,伙计们,非常感谢你们。