java.io.NotSerializableException将hasMany更改为ArrayList

时间:2012-06-27 14:04:18

标签: grails serialization validation

class Author {

    String name

    ArrayList<Book> books = []

    static hasMany = [books: Book]

    static mapping = {
      books cascade: "all-delete-orphan"
    }

}

尝试保存对象时发生错误。 “java.io.NotSerializableException”。有任何想法吗?我是否需要在类中实现Serializable?如果是这样,为什么?

1 个答案:

答案 0 :(得分:1)

以下是解决方案:

class Author {

    String name

    SortedSet books

    static hasMany = [books: Book]

    static mapping = {
      books cascade: "all-delete-orphan"
    }

}

   class Book implements Comparable {
      String title

      static belongsTo = [author: Author]

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

   }