本文介绍了HQL中不支持Postgres中的LIMIT?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
def items = Item.executeQuery('SELECT i FROM Item as i LIMIT 30 OFFSET 0')
我的后端数据库是Postgres的。但是,我得到了:
org.hibernate.hql.ast.QuerySyntaxException:意外标记:第1行附近的LIMIT列。 。
显然,错误告诉HQL不支持LIMIT。我如何使它工作?换句话说,我如何让HQL执行Postgres良好支持的本地SQL?
你可以做到也可以 > def items = Item.list(offset:0,max:30)
由于您使用的是HQL,因此我已根据以下评论编辑了我的答案;这两种方法都可行
def items = Item.executeQuery('SELECT i FROM Item as i',[max:30,offset :0])
I am having error using the following code in Grails executing HQL to get the first 30 Item objects:
def items = Item.executeQuery('SELECT i FROM Item as i LIMIT 30 OFFSET 0')
my backend database is Postgres. However, I got:
org.hibernate.hql.ast.QuerySyntaxException: unexpected token: LIMIT near line 1, column ...
Obviously, the error tells that LIMIT is not supported by HQL. How do I make it work? In other words, how do I make HQL execute native SQL that is well supported by Postgres?
解决方案
you could do it the Grails/GORM way using list also
def items = Item.list(offset:0, max:30)
Since you are using HQL I have edited my answer based on comment below; either approach would work
def items = Item.executeQuery('SELECT i FROM Item as i', [max: 30, offset: 0])
这篇关于HQL中不支持Postgres中的LIMIT?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!