Announcement Announcement Module
Collapse
No announcement yet.
Best way of setting relationships Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Best way of setting relationships

    Hello,

    I've been working with grails for some months and now I understand better how it works and how things should be done, but I still have some doubts with GORM relationships.

    Until know this is the way we make the relations:

    one-to-one
    We haven't used this one too much, but I think is the recommended one for this kind of relations.

    Code:
    class Empresa{
       String nombre
       
       static hasOne = [localizacion:Localizacion]
    }
    
    class Localizacion{
       String direccion
       String provincia
    }
    one-to-many
    I have some doubts with this type of relationships. Is the belongsTo mandatory? If not, when should it be used?
    What I've read is that setting the belongsTo property allows deletes to be cascaded and without it, saves and updates are cascaded but not deleted. Am I wrong?

    Code:
    class Empresa{
       String nombre
       static hasMany=[sedes:Sede]
    }
    
    class Sede{
       String nombreSede
       String territorio
       static belongsTo=[empresa:Empresa]
    }
    many-to-many
    We have used this kind of relationships, but creating or own join table because we needed to add some properties to the relation.
    I think that to let grails manage this relationship you'll need just to set hasMany in both sides and maybe a belongsTo in one of the sides, but I don't have this too clear.
    Anyone could send some examples on how to use it? Also I would want to know if our way of making this relationship (two one-to-many relationships) is ok or not.

    If someone could suggest me the best way to make all the types of the relationships it would be great.

    Thanks!

    P.S. I have read the documentation but still have some doubts...

  • #2
    take a read through this blog post and the other two (part I and III)

    Comment


    • #3
      Thanks for the answer, but I'd already read them and some of my conclusions come from them. Just wanted to check if they're the best ones.

      Comment

      Working...
      X