Search code examples
grailsgrails-ormgrails-2.0grails-domain-class

What is the best way to implement self-referenced one-to-many relationships in Grails?


I want to implement a follower following relationship in Grails GORM. One implementation could be:

class User {
  String name
}

class Follower {
  User user 
  User follower
}

OR:

class User {
  String name

  static hasMany = [follower: User, following: User]
}

What is the best way to implement that a user can have many follower and a user can follow many users?


Solution

  • class User { String name

    static hasMany = [follower: User, following: User] }

    this will be better because you can easily create instance of new follower and then can easily to addTo method to add it as hasmany