我有这样的关系:
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
}
我有以下带有以下参数的查询,其中大部分用于使用 flexigrid 进行分页。此查询获取与viaThing
的特定实例相关联的所有实例。因此,如果我的is实例期望我的查询返回并且:Bar
Foo
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
子句,所以我只是插入了字符串。问题是结果没有按照我指定的顺序排列。使用ORDER BY id
Grails 时告诉我id
是模棱两可的。
知道变量sortname
将始终是 的成员Thing
,我如何指定要排序的内容?
我尝试过的一些事情:
ORDER BY Thing.id // Fail
ORDER BY f.things.id // Fail
ORDER BY things.id // FAIL!