(快速参考)

executeQuery

用途

执行 HQL 查询

示例

// simple query
Account.executeQuery("select distinct a.number from Account a")

// using with list of parameters
Account.executeQuery("select distinct a.number from Account a " +
                     "where a.branch = ? and a.created > ?",
                     ['London', lastMonth])

// using with a single parameter and pagination params
Account.executeQuery("select distinct a.number from Account a " +
                     "where a.branch = ?", ['London'],
                     [max: 10, offset: 5])

// using with Map of named parameters
Account.executeQuery("select distinct a.number from Account a " +
                     "where a.branch = :branch",
                     [branch: 'London'])

// using with Map of named parameters and pagination params
Account.executeQuery("select distinct a.number from Account a " +
                     "where a.branch = :branch",
                     [branch: 'London', max: 10, offset: 5])

// same as previous
Account.executeQuery("select distinct a.number from Account a " +
                     "where a.branch = :branch",
                     [branch: 'London'], [max: 10, offset: 5])

// tell underlying Hibernate Query object to not attach newly retrieved
// objects to the session, will only save with explicit `save`
Account.executeQuery("select distinct a.number from Account a",
                     null, [readOnly: true])

// time request out after 18 seconds
Account.executeQuery("select distinct a.number from Account a",
                     null, [timeout: 18])

// have Hibernate Query object return 30 rows at a time
Account.executeQuery("select distinct a.number from Account a",
                     null, [fetchSize: 30])

// modify the FlushMode of the Query (default is `FlushMode.AUTO`)
Account.executeQuery("select distinct a.number from Account a",
                     null, [flushMode: FlushMode.MANUAL])

说明

executeQuery 方法允许执行任意 HQL 查询。当查询选择各个字段或计算值时,HQL 查询可以返回域类实例或指定数据字段的 `Array`。基本语法为

Book.executeQuery(String query)
Book.executeQuery(String query, List positionalParams)
Book.executeQuery(String query, List positionalParams, Map metaParams)
Book.executeQuery(String query, Map namedParams)
Book.executeQuery(String query, Map namedParams, Map metaParams)

参数

  • query - HQL 查询

  • positionalParams - 位置化查询参数的 `List`

  • namedParams - 命名查询参数的 `Map`

  • metaParams - 分页参数 maxoffset 以及 Hibernate 查询参数 readOnlyfetchSizetimeoutflushMode 的 `Map`