grails域默认按上次更新字段排序

时间:2014-08-10 07:05:49

标签: mysql sorting grails gorm relationship

我有一个grails域hasMany和belongsTo关系,如下所示:

我想根据“提交”排序' ' LASTUPDATED' '降序'

class User {
  String username
  String password

  static hasMany = [ submissions: Submission ]

  static mapping = {
     sort submissions: 'desc'  // This doesn't do anything?
  }
}

class Submission {
  String reference
  Date dateCreated
  Date lastUpdated

  static belongsTo = User
}

所以每当我收集用户提交的内容时,我希望它默认排序为lastUpdated desc。等效的mysql语句将是以下

select (fields) from submission order by last_updated desc;

我错过了什么吗?

非常感谢!

2 个答案:

答案 0 :(得分:0)

我认为你应该阅读如何set default sort order in assosiation

答案 1 :(得分:0)

因为这是单向关系,所以以下解决方案完美无缺!

class User {
  String username
  String password

  SortedSet submissions

  static hasMany = [ submissions: Submission ]

}

class Submission implements Comparable {
  String reference
  Date dateCreated
  Date lastUpdated

  static belongsTo = User

  @Override
  public int compareTo(obj) {
        obj.lastUpdated.compareTo(lastUpdated)
  } 

}