Вопрос

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