16

我正在尝试hasMany使用映射语句设置我的属性的默认排序。我正在关注 grails 文档,但它对我不起作用(grails 1.3.5)。我的代码如下所示:

class Note {
    Calendar    sendDate
    static belongsTo = Message
}

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

错误消息如下所示:

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

您在我的代码中看到任何错误吗?

4

2 回答 2

21

一些可能有助于解决问题的事情:

  • 你真的需要CalendarsendDate属性使用 a 吗?大多数时候,人们会使用java.util.Date. 更改字段类型以Date解决问题吗?
  • 我用您的映射运行了一个示例,但出现错误。尝试将您的Message静态mapping闭包更改为:

    static mapping = {
        notes sort: 'sendDate', order: 'desc'
    }
    
于 2010-11-12T13:43:46.423 回答
11

这个页面讲述了对象关系映射的所有内容,我的应用程序也有类似的问题。我这样解决了:

class Note implements Comparable {
  Calendar sendDate
  static belongsTo = Message

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

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

希望这会有所帮助!

于 2011-03-10T14:53:27.753 回答