grails hasOne vs直接成员变量 [英] grails hasOne vs direct member variable

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

问题描述

  class Person {
Address address
}

我也可以声明为

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

第二种方法是将外键移到地址表而不是人表中。



实际的好处是什么缺点)这样做与其他方式?据我所知,他们都会使用外键,这只是外键存在的问题。

解决方案

如果外键存在于地址表上,那么该地址只能有一个人。
如果外键在人员表上,则多个人可以具有相同的地址。



这不是什么方式更好/更糟。这是关于什么是建模数据的正确方法。


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

class Person {
    Address address
}

I could also declare it as

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

The second way would move the foreign key to the Address table rather than the person table.

What are the practical benefits (or disadvantages) of doing this one way vs the other? As far as I understand, they will both use foreign keys, it's just a matter of where the foreign key lives.

解决方案

If the foreign key exists on the address table, then that address can only have one person. If the foreign key is on the person table, multiple persons can have the same address.

It's not about what way is better/worse. It's about what is the correct way to model your data.

这篇关于grails hasOne vs直接成员变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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