简体   繁体   中英

grails domain default sort by last updated field

I have a grails domain hasMany and belongsTo relationship as the below :

I am wanting to sort based on 'Submission' 'lastUpdated' 'desc'

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
}

So everytime I collect a User's submissions, I would like it default sorted as lastUpdated desc. The equivalent mysql statement would be the following

select (fields) from submission order by last_updated desc;

Am i missing something?

Thanks greatly!

I think you should read how to set default sort order in assosiation

From GORM documentation

Finally, you can configure sorting at the association level:

class Airport {
    ...
    static hasMany = [flights: Flight]

    static mapping = {
        flights sort: 'number', order: 'desc'
    }
}

Because this is a unidirectional relationship, the following solution works perfectly!

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)
  } 

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM