Grails hasOne vs. belongsTo

允我心安 提交于 2019-12-01 01:27:51

问题


To create one-to-one relationships in Grails I can do:

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

In this case the Address table has the key to its person. I could also do:

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

This gives the same result.

What is the difference between my two samples using hasOne and belongsTo?


回答1:


hasOne indicates that there is a bi-directional one-to-one relationship where the child table has the parent's foreign key, as in your example.

belongsTo is used to control cascades by indicating that the class belongs to the specified class. In your example, deleting a given Person would cascade the delete to any associated Addresses.



来源:https://stackoverflow.com/questions/31166149/grails-hasone-vs-belongsto

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!