Why grails throwing null pointer exception while accessing hasMany relationship first time?

前端 未结 2 873
春和景丽
春和景丽 2021-02-18 15:48

I have a strange problem.
I have two domain classes User and Post with fields:

class User {
  String name
  static hasMany = [pos         


        
2条回答
  •  难免孤独
    2021-02-18 16:22

    You can prevent this by explicitly declaring your collection property (with a value) alongside your mapping:

    class User {
        String name
        Set posts = []
        static hasMany = [posts: Post]
    }
    

    You can define the collection type you need. The default is Set, but if you need to maintain order, you might consider List or SortedSet.

提交回复
热议问题