Grails hasOne vs. belongsTo [英] Grails hasOne vs. belongsTo

查看:143
本文介绍了Grails hasOne vs. belongsTo的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Grails中创建一对一的关系我可以这样做:

  class Person {
static hasOne = [地址:地址]
}

在这种情况下,地址表具有它的人。我也可以这样做:

  class地址{
static belongsTo = [person:Person]
}

这给出了相同的结果。

我的两个样本使用 hasOne belongsTo

解决方案

hasOne 表示有一个双向一对一关系,其中子表具有父项的外键,如您的示例所示。



belongsTo 用于通过指示该类属于指定的类来控制级联。在你的例子中,删除给定的 Person 会将delete删除到任何关联的 Address es。


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?

解决方案

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.

这篇关于Grails hasOne vs. belongsTo的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆