Grails 对具有多个关系的类的对象进行排序
我有这样的域名Grails 对具有多个关系的类的对象进行排序,grails,gorm,Grails,Gorm,我有这样的域名 class A{ String mainAddress static hasMany = [Bs: B] static mapping = { mainAddress formula: ('select ad.zip from B ad where ad.a_id=id') } } Class B{ String zip static belongsTo = [a: A] } let A1 ->B1 ->B2 and A2 ->B3
class A{
String mainAddress
static hasMany = [Bs: B]
static mapping = {
mainAddress formula: ('select ad.zip from B ad where ad.a_id=id')
}
}
Class B{
String zip
static belongsTo = [a: A]
}
let A1 ->B1
->B2
and A2 ->B3
->B4
A3 ->B5
->B6
etc..
现在我想根据B1、B3、B5等的zip进行排序
编辑:我做了一些有问题的更改,然后尝试访问
a.mainAddress
如果我理解您的意思,您希望按zip对属于某个对象的B个对象进行排序,则此代码应适用于您:
def a = .. instance of A object
def listOfB = a.Bs.sort{it.zip}
或者,如果要在数据库中对其进行排序:
您应该向B类添加另一个静态映射:
此外,公式应为:
formula: '(select ad.zip from B ad where ad.a_id=this.id)'
而不是:
formula: ('select ad.zip from B ad where ad.a_id=id')
因为您没有指定要获取的对象的id。您已经尝试了什么?你尝试过的东西有什么不起作用?您使用的是标准吗?HQL?原始SQL?我正在尝试将原始SQL作为公式。我将在片刻内更新。然后,您应该将其与结果一起放入问题中,以及您在结果中遇到的问题,并请求帮助解决该问题。否则,这个问题太宽泛了,只是说为我写代码。我已经更新了我的问题,我已经尝试过了。这里需要做一些小的更改“从B ad中选择ad.zip,其中ad.a_id=id”,引用查询。。。。ad.a_id=id'并且它在没有这个id而不是相同id的情况下工作?
formula: ('select ad.zip from B ad where ad.a_id=id')