Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Hibernate 如何在Grails HQL语句中按顺序排序_Hibernate_Grails_Sql Order By_Hql - Fatal编程技术网

Hibernate 如何在Grails HQL语句中按顺序排序

Hibernate 如何在Grails HQL语句中按顺序排序,hibernate,grails,sql-order-by,hql,Hibernate,Grails,Sql Order By,Hql,我有这样的关系: class Foo { static hasMany = [bars: Bar, things: Thing] } class Bar { // Has nothing to tie it back to Foo or Thing } class Thing { // Has nothing to tie it back to Foo or Bar } 我有以下带有以下参数的查询,其中大部分用于FlexGrid的分页。此查询通过Foo获取与Bar

我有这样的关系:

class Foo {
    static hasMany = [bars: Bar, things: Thing]
}

class Bar {
    // Has nothing to tie it back to Foo or Thing
}

class Thing {
    // Has nothing to tie it back to Foo or Bar
}

我有以下带有以下参数的查询,其中大部分用于FlexGrid的分页。此查询通过
Foo
获取与
Bar
的特定实例关联的
Thing
的所有实例。因此,如果我的
Bar
实例是
Bar1
,那么我的查询将返回
Thing1
Thing2

def obj = Bar.get( 1 ) // Get Bar1
def max = params.int( "max" ) ?: 100 // Default max returned results is 100 unless otherwise specified
def offset = params.int( "offset" ) ?: 0 // Default offset is 0 unless otherwise specified
def sortname = params.sortname ?: "id" // Default is id, but could be any member of Thing that is not a "hasMany"
def sortorder = params.sortorder ?: "ASC" // Default is ASC, but could be DESC

def namedParams = [ obj: obj, max: max, offset: offset ]

Thing.executeQuery( "SELECT DISTINCT f.things FROM Foo f INNER JOIN f.things things INNER JOIN f.bars bars WHERE bars =:obj ORDER BY ${sortname} ${sortorder}", namedParams )
Hibernate不允许使用命名参数来指定
ORDER BY
子句,所以我只是插入了字符串。问题是结果没有按照我指定的顺序排列。使用
orderbyid
Grails告诉我
id
是不明确的

知道变量
sortname
将始终是
Thing
的成员,如何指定排序依据?

我尝试过的一些事情:

ORDER BY Thing.id // Fail
ORDER BY f.things.id // Fail
ORDER BY things.id // FAIL!
查询应为:

SELECT DISTINCT thing FROM Foo f 
INNER JOIN f.things thing 
INNER JOIN f.bars bar 
WHERE bar = :obj 
ORDER BY thing.id

i、 e.您应该在select子句中使用实体的别名,而不是其路径,并在order by子句中使用相同的别名。

!谢谢你的帮助。HQL与SQL并没有太大的不同,但它的不同足以给我带来一些问题。不过,我现在已经掌握得很好了:)