开发者

Grails default sort of "hasMany" domain attributes

开发者 https://www.devze.com 2023-01-24 14:10 出处:网络
I\'m trying to set default sort of my hasMany attribute using mapping statement. I\'m following the grails doc but it doesn\'t work for me (grails 1.3.5). My code looks like:

I'm trying to set default sort of my hasMany attribute using mapping statement. I'm following the grails doc but it doesn't work for me (grails 1.3.5). My code looks like:

class Note {
    Calendar    sendDate
    static belongsTo = Message
}

class Message {
    static  hasMany = [notes: Note]
    static mapping = {
        notes sort:'sendDate desc'
    }
}

The error message looks like:

...
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'notes0_.sendDate' in 'o开发者_Python百科rder clause'
        at com.mysql.jdbc.Util.handleNewInstance(Util.java:409)
        at com.mysql.jdbc.Util.getInstance(Util.java:384)
...

Do you see any mistakes in my code?


A couple things that may help fix the problem:

  • Do you really need to use a Calendar for the sendDate property? Most of the time, one would use a java.util.Date. Does changing the field type to a Date fix the issue?
  • I ran an example with your mappings and got an error. Try changing your Message static mapping closure to this:

    static mapping = {
        notes sort: 'sendDate', order: 'desc'
    }
    


This page tells all about Object Relational Mapping, I had a similar problem with my app. I solved it like so:

class Note implements Comparable {
  Calendar sendDate
  static belongsTo = Message

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

and

class Message {
  SortedSet notes
  static  hasMany = [notes: Note]
}

Hope this helpes!

0

精彩评论

暂无评论...
验证码 换一张
取 消