我试图通过第3层关系对数据进行排序,但我无法让它发挥作用。我已成功使用别名来查询2级关系。这就是我的查询设置方法
def query = Application.where {}
query.createAlias("applicationDegrees", "appDegrees");
query.createAlias("applicationDegrees.degree", "degree");
query.createAlias("applicationDegrees.applicationDegreeMajors", "appMajors");
query.createAlias("applicationDegrees.applicationDegreeMajors.major", "major");
return query.list([sort: "degree.code", order: "desc"]); //WORKS
return query.list([sort: "major.code", order: "desc"]); //DOES NOT WORK
从上面的示例代码中可以看出,我可以按appDegrees和degree排序。当我尝试更进一步,根据appMajors或major的关系进行排序时,appDegrees的关系不能正确排序。根本不应用任何排序,但也不会抛出任何错误消息。
这是我的数据模型
class Application {
Set<ApplicationDegree> applicationDegrees = []
static hasMany = [applicationDegrees: ApplicationDegree]
}
class ApplicationDegree {
Application app
Degree degree
Set applicationDegreeMajors = []
static hasMany = [applicationDegreeMajors: ApplicationDegreeMajor]
static belongsTo = [Application, Degree]
}
class ApplicationDegreeMajor {
ApplicationDegree applicationDegree
Major major
static belongsTo = [ApplicationDegree, Major]
}
如何创建别名以便我可以按Major排序?
Grails 3.1.4
答案 0 :(得分:0)
我想可以在这里找到答案(和解决方法):https://forum.hibernate.org/viewtopic.php?t=947709
似乎不支持嵌套别名,因此我建议将其替换为:
query.createAlias("applicationDegrees", "appDegrees");
query.createAlias("appDegrees.degree", "degree");
query.createAlias("appDegrees.applicationDegreeMajors", "appMajors");
query.createAlias("appMajors.major", "major");