16

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 'order 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?

Rob Hruska
  • 118,520
  • 32
  • 167
  • 192
Mateo
  • 193
  • 1
  • 2
  • 5
  • export your schema and see of table and columns exist http://www.grails.org/doc/1.1/ref/Command%20Line/schema-export.html – Aaron Saunders Nov 12 '10 at 13:46
  • @Aaron - When I tried this out with the default hsqldb in-memory database, it only created one column for the `Calendar` field, and everything looked normal (I had thought maybe it was creating two columns to store, e.g., the time zone or something, but it didn't appear that it was). – Rob Hruska Nov 12 '10 at 16:21

2 Answers2

21

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'
    }
    
Rob Hruska
  • 118,520
  • 32
  • 167
  • 192
  • 1
    Unfortunately both didn't work for me. But I've solved the problem that I defined the relation from both sides. I've just add "Message message" to the Note class and it works.:-) But anyway thanks for your interest!:-) – Mateo Nov 12 '10 at 14:25
  • 2
    @Mateo - Good to know. I'd recommend posting whatever you found out for a solution as an answer and then accepting it (in a couple days) so that others may know how to solve the problem if they encounter it. – Rob Hruska Nov 12 '10 at 16:22
11

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!

demon101
  • 544
  • 1
  • 11
  • 39
BadSkillz
  • 1,993
  • 19
  • 37