I have a query like that:

SELECT *
FROM MyTable mt
WHERE mt.firstCondition = :var1
AND mt.secondCondition = :var2

var1 is an Integer and var2 is a String. I want to discard the condition for var1, if I receive -1 on it. So, if var1 is anything bellow zero, the query becomes:

SELECT *
FROM MyTable mt
WHERE mt.secondCondition = :var2

Can I make that change on a NamedQuery?

有帮助吗?

解决方案

Hibernate parses named queries at startup which is what gives the performance boost as Hibernate doesn't have to do it each time the query is executed. So what I'm saying is you can't dynamically change a named query, it will defeat its purpose if you could. What you can do is place some conditions in your named query in question to cater for your needs. Luis LL provided you the condition so I wont repeat.

其他提示

like this?

SELECT *
FROM MyTable mt
WHERE (mt.firstCondition = :var1 OR :var1 = -1) AND mt.secondCondition = :var2
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top