2012-12-17 42 views
5

這裏是我的HQL:括號HQL不ntranslated到SQL

Query query = createQueryOnCurrentSession("DELETE Email e " + 
       "where " + 
       "(status = :sent and creationTime <= :creation)" + 
       "or " + 
       "(status = :error and maxEttempts >= :maxEttempts)"); 

這裏是生成的SQL:

delete from `email` where `status`=? and `creation_time`<=? or `status`=? and `attempts`>=? 

問:爲什麼括號不是在SQL? 我希望它是:

delete from `email` where (`status`=? and `creation_time`<=?) or (`status`=? and `attempts`>=?) 

可能是作爲替代,我將在2個請求刪除?

delete from `email` where `status`=? and `creation_time`<=? 
delete from `email` where `status`=? and `attempts`>=? 
+0

試圖逃避括號就像這樣:'\(' –

+0

單'」 \(「'不編譯(當然)和雙'」\\(「'throw exception:'org.hibernate.QueryException:unexpected char:'\'[DELETE com.grroo.model.Email e where \ status =:sent and creationTime <=:creation \)或\(status =:error and attempts> =:maxAttempts \)]' – urir

+0

我想你可以附加查詢字符串作爲您的條件,然後執行 –

回答

8

這實際上是一個功能。

由於and優先於or,hibernate知道它,並刪除括號。

你不需要那些括號。

+0

我知道它就像在某些數據庫中......但它是我將使用的所有數據庫的標準嗎?你可以參考嗎?我找不到它... – urir

+0

我不確定它是否是一個固定的標準,但必須DBs使用它。 – Jaanus

+0

這正是我困擾的問題。也許有辦法告訴休眠離開括號? – urir

2

邏輯運算符,其優先級高於邏輯運算符OR

運算符優先級的高階,按照鏈接

http://dev.mysql.com/doc/refman/5.0/en/operator-precedence.html 
http://msdn.microsoft.com/en-us/library/ms190276.aspx 

http://docs.oracle.com/html/A85397_01/operator.htm#997691 
+0

這裏只有最大的3個數據庫提供者,當然使用這個標準。 – Jaanus