【问题标题】:hql not deleting query okhql 不删除查询确定
【发布时间】:2013-06-05 22:05:35
【问题描述】:
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);
}

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

【问题讨论】:

  • 为什么会有这么复杂的查询,而不仅仅是delete from Attendance att where att.dat= :dat and att.cls_id= :cls_id
  • 我改变了它并在它不起作用时变得复杂,这是我的愚蠢错误,可能是我太困而忘记执行了。对不起各位,非常感谢你们。

标签: java hibernate spring-mvc hql


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2011-08-21
    • 2013-09-08
    • 2020-07-18
    • 2016-05-20
    • 2011-12-05
    • 1970-01-01
    • 2013-10-06
    • 1970-01-01
    • 2020-09-22
    相关资源
    最近更新 更多