2012-12-17 33 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