2017-06-15 40 views
0

最初在數據庫中計數爲0。每當有需求時,我都需要更新計數。如何使用hibernate在數據庫中增加計數?

更新查詢

這裏計數最初是0

假設詮釋計數= 10; query.setParameter(「count」,count); 它只會更新一次 輸出是11.下一次相同的查詢更新相同的輸出11會來,但我需要12是輸出。

String hql = "Update User f set f.count =:count + 1 where f.userId =:userId"; 
     Query query = session.createQuery(hql); 
     query.setParameter("userId", userId); 
     //query.setParameter("count", count); 
     result = query.executeUpdate(); 

count is not updated。看起來積極的重播。

謝謝..!

+0

此代碼段不會運行。 「:count」參數從未設置爲查詢對象。 – SudoRahul

+0

@SudoRahul我加了query.setParameter(「count」,count);但它沒有更新 – sweety

+0

你是否通過舊計數值? – SudoRahul

回答

3

你試過了嗎?

"Update User f set f.count=(f.count + 1) where f.userId =:userId"; 
+0

非常感謝你的工作 – sweety

相關問題