2013-06-05 84 views
0
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); 
} 

我想刪除具有特定條件的現有行,因爲我保存的對象可能已經存在於表中。但是,該刪除不起作用,重複值被插入。請讓我知道哪部分代碼是錯誤的。hql沒有刪除查詢正常

+1

爲什麼這樣一個複雜的查詢,而不是僅僅從'考勤ATT刪去ATT.DAT =:DAT和att.cls_id =:cls_id'? –

+0

我改變了它,當它不工作時變得複雜,這是我愚蠢的錯誤,可能是我太困了,忘了執行。對不起,夥計們,非常感謝。 – Aadam

回答

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();