How does hasMany and hasOne work in Grails?

I am having problems defining one-to-one and one-to-many relationships with domain classes. I have the following domain classes

class Team {
    static hasMany = [players: Player]
    static hasOne = [coach: Coach]
}

class Person {
    String name
}

class Player extends Person {
}

class Coach extends Person {
}

      

So my questions are:

1- Do I need to declare a variable team

in the player and in the trainer?

2- Do I also need to announce belongsTo

?

3. Given the above classes, is it preferable to use hasOne?

Thank.

+2


source to share


2 answers


  • Only if you want to navigate easily through player.team and coach.team
  • Depends on whether you want to update / remove cascades. I think not, since the removal of a coach or player should not remove a team or vice versa?
  • hasOne looks reasonable to team> trainer, but it doesn't exist in Grails 1.1.1 or below. It might be in 1.2 (but it's not in the reference).

amuses



Lee

+1


source


There is a small bug with leebutt's answer.



  • A cascade is the other way around: if your coach / player belongs to the Team, then the removal of the team will be a cascade and the removal of the coach / player.
+2


source







All Articles