如何将列表作为参数添加到grails中的sqlQuery?

时间:2014-07-10 06:21:05

标签: grails groovy

我正在编写一个查询,并希望设置参数,这是一个字符串列表。

我的查询是

def strQuery = """SELECT * from user u where u.status in (?)"""

def session = sessionFactory.getCurrentSession()

def resultList = session.createSQLQuery(strQuery)
        .setParameter(0, UserStatus.List)
        .list();

哪里

public enum UserStatus {
    ACTIVE("Active"),
    ARCHIVE("Archived"),

    public static final List<UserStatus> List = [ACTIVE,ARCHIVED]

}

但我得到了一个例外

ERROR org.hibernate.util.JDBCExceptionReporter  - ERROR: operator does not exist: character varying = bytea
  Hint: No operator matches the given name and argument type(s). You might need to add explicit type casts.

1 个答案:

答案 0 :(得分:1)

要绑定列表,您必须使用带有命名参数的setParameterList

setParameter仅适用于单一值。

def strQuery = """SELECT * from user u where u.status in (:statusList)"""

def session = sessionFactory.getCurrentSession()

def resultList = session.createSQLQuery(strQuery)
    .setParameterList('statusList', UserStatus.List)
    .list();