多个连接排序问题

时间:2010-06-30 20:02:46

标签: grails groovy

我在使用多个连接表进行排序时遇到问题。例如:

class Account {

  static hasMany = [subscriptions: Subscription]

  static mapping = {
    subscriptions fetch: 'join'
    subscriptions sort: 'magazine'
  }

}

class Subscription {

  static belongsTo = [account:Account, magazine: Magazine]

  static maping = {
    magazine fetch: 'join'
    magazine sort: 'name'
  }

}

class Magazine {
  String name  
  static mapping = {
    sort name: 'desc'
  }
}

当通过magazine.id调用someAccount.subscriptions生成查询命令时。有没有办法通过magazine.name获得订单?我尝试更改订阅排序:'magazine.name',但得到错误,没有这样的属性。

以下:http://grails.org/doc/latest/guide/single.html#5.5.3默认排序顺序我尝试通过从Account中删除排序并仅在Subscription中保留排序但最终从结果查询中完全删除“order by”,将排序移动到关联级别。有任何想法吗?感谢。

1 个答案:

答案 0 :(得分:1)

尝试在Magazine类中实现compareTo方法:

class Magazine implements Comparable {
  String name  

  int compareTo(obj) {
      name.compareTo(obj.name)
  }
}

然后订阅

class Subscription implements Comparable {

    static belongsTo = [account:Account, magazine:Magazine]
    static constraints = {
    }
    int compareTo(obj) {
       return magazine.compareTo(obj.magazine)
    }
}

然后使订阅成为Account中的SortedSet:

class Account {
    SortedSet subscriptions
    static hasMany = [subscriptions:Subscription]
}