将 IN 子句列表添加到 JPA 查询 [英] Adding IN clause List to a JPA Query
问题描述
我构建了一个如下所示的 NamedQuery:
I have built a NamedQuery that looks like this:
@NamedQuery(name = "EventLog.viewDatesInclude",
query = "SELECT el FROM EventLog el WHERE el.timeMark >= :dateFrom AND "
+ "el.timeMark <= :dateTo AND "
+ "el.name IN (:inclList)")
我想要做的是用项目列表而不是一个项目填写参数:inclList.例如,如果我有一个 new List
我如何在 :inclList 参数中得到它?它只让我编码一个字符串.例如:
What I want to do is fill in the parameter :inclList with a list of items instead of one item. For example if I have a new List<String>() { "a", "b", "c" }
how do I get that in the :inclList parameter? It only lets me codify one string. For example:
setParameter("inclList", "a") // works
setParameter("inclList", "a, b") // does not work
setParameter("inclList", "'a', 'b'") // does not work
setParameter("inclList", list) // throws an exception
我知道我可以只构建一个字符串并从中构建整个查询,但我想避免开销.有没有更好的方法来做到这一点?
I know I could just build a string and build the whole Query from that, but I wanted to avoid the overhead. Is there a better way of doing this?
相关问题:如果List非常大,有没有什么好的方法来构建这样的查询?
Related question: if the List is very large, is there any good way of building query like that?
推荐答案
当使用带有集合值参数的 IN
时,您不需要 (...)
:
When using IN
with a collection-valued parameter you don't need (...)
:
@NamedQuery(name = "EventLog.viewDatesInclude",
query = "SELECT el FROM EventLog el WHERE el.timeMark >= :dateFrom AND "
+ "el.timeMark <= :dateTo AND "
+ "el.name IN :inclList")
这篇关于将 IN 子句列表添加到 JPA 查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!