开发者

How to express "where value is in dynamic list" in HQL/GORM?

开发者 https://www.devze.com 2023-01-02 09:56 出处:网络
For a grails application, I need to find a list of objects whose \"attr\" is one in a dynamic list of strings. The actual HQL query is more complex, but the bit I need help with is this:

For a grails application, I need to find a list of objects whose "attr" is one in a dynamic list of strings. The actual HQL query is more complex, but the bit I need help with is this:

def result = MyObject.executeQuery("select o from MyObject as o where o.attr in :list",
    [list: aListOfStrings])

This is obviously not the right syntax, Grails throws it back at me as an "unexpected token", being the :list parameter.

Is this possibl开发者_Go百科e in HQL? I don't particularly want to use Criteria in this part of the codebase.


Put :list in parens:

def result = MyObject.executeQuery(
    "select o from MyObject as o where o.attr in (:list)",
    [list: aListOfStrings])


I'm not sure how the performance compares, but I was able to do this straight in GORM with the in keyword in a where query:

def users = MyObject.where { attr in aListOfStrings }


This works for me like champs.

Order.executeQuery("from Order o where o.categoryId = :ocatId and o.part in (:partsList) and o.status in (:statusList)", [ocatId : categoryId, partsList:partsList, statusList:statusList] )

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号