我有一个使用HQL的列表方法.如何将分页和排序参数添加到此查询?
def list = {
def termList
log.info "Getting terms for product: $params.productId"
params.max = 10
def p = Product.get(params.productId)
if (p) {
log.info "Product found: $p.name"
termList = Term.executeQuery("select distinct t from Term as t join t.definitions def join def.definitionProducts dp where dp.product=?",
p )
} else {
log.info "No Product found"
termList = Term.list(params)
}
log.info "Terms found: $termList.size"
[ termInstanceList: termList, termInstanceTotal: termList.size]
}
Run Code Online (Sandbox Code Playgroud)
我遇到了同样的问题; 虽然您可以在executeQuery参数中指定max和offset,但将忽略排序和顺序.为了实现这一点,我必须在HQL查询本身中指定排序和顺序.生成的查询应如下所示:
"select distinct a.number from Account a where a.branch = :branch order by a.id asc"
Run Code Online (Sandbox Code Playgroud)
首先,在可排序的列中,您需要更改property属性以引用要查询的域.因此对于"帐户a",请执行以下操作:
<g:sortableColumn property="a.id" title="Id"/>
Run Code Online (Sandbox Code Playgroud)
接下来,您将需要更改HQL查询.不幸的是,您似乎无法在order by子句中使用命名参数,因此您需要手动执行此操作.首先清理params.order和params.sort可能是一个好主意.
"select distinct a.number from Account a where a.branch = :branch order by " + params.sort + " " params.order
Run Code Online (Sandbox Code Playgroud)
这对我有用,我真的希望有更好的方法.
| 归档时间: |
|
| 查看次数: |
8357 次 |
| 最近记录: |