grails hasOne vs direct member variable

前端 未结 4 952
余生分开走
余生分开走 2021-02-04 02:10

Let\'s say I have a grails domain class that looks like

class Person {
    Address address
}

I could also declare it as

class P         


        
4条回答
  •  难免孤独
    2021-02-04 02:46

    I suggest the following...

    class Person {
      ...
      static hasOne = [address: Address]
    }
    
    class Address {
        ...
        static belongsTo = [person: Person]
    }
    

    A person has one address and the address belongs to one person.

    By this way, when you delete a person, the address will be deleted too, without problems.

    I think this is the better way to do this.

提交回复
热议问题