grails / gorm 支持 1 - 许多关联中的不同父实体
我有以下“顶级”(父)域实体:
客户
公司
联系人
以及以下子实体:
地址
每个顶级域实体之间存在一对多关系:
客户 -> 客户地址
公司->地址
联系方式->地址
即客户、公司或联系人可以有一个或多个地址。
不幸的是我不知道如何在 grails / gorm 中对此进行建模。看来我只能在地址中定义一个父级或belongsTo声明,即我无法使用以下方式声明地址:
Address {
Customer parent //??
Company parent //??
Contact parent //??
}
有人可以告诉我是否缺少某些内容或者是否可以以受支持的方式定义这种类型的关系?
谢谢,
考珀
I have the following "top-level" (parent) domain entities:
Customer
Company
Contact
And the following child entity:
Address
There is one to many relationship between each of the top level domain entities:
Customer -> Address
Company -> Address
Contact -> Address
i.e. a customer, company or contact can have one or more addresses.
Unfortunately I do not know how to model this in grails / gorm. It seems I can only define one parent or belongsTo declaration in address i.e. I was not able to declare Address using:
Address {
Customer parent //??
Company parent //??
Contact parent //??
}
Can someone tell me if I am missing something or if it's possible to define this type of relationship in a supported way?
Thanks,
cowper
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如 Tim 指出的那样,您应该能够使用
belongsTo
的数组版本:如果实体可以共享公共地址,则可能会改变您配置删除的方式。
另一种选择是让这三个类 从超类继承属性,但无论这对您的情况是否有意义,我不知道(看起来不像)。
You should be able to use the array version of
belongsTo
as Tim pointed out:If entities can share a common address may change the way you configure deletes.
Another option is to have those three classes inherit the property from a superclass, but whether or not that makes sense in your case, I don't know (it kind of doesn't look like it).
在我们的应用程序中,我们有几个需要地址的实体。但我们选择以多对多关系对它们进行建模。
地址看起来像这样
对于每个“父母”,我们提供一个吸气剂。您可以在上面的代码中看到 getCompanies()。如果每个地址只有 1 个公司,那么只需让该 getter 返回 1 个公司而不是 Set。 Company 内部则相反,我们有一个 getAddresses()。
例如,公司地址看起来像这样......
In our application we have several entities that need addresses. But we've chosen to model them in a many-to-many relationship.
Address looks like this
And for each "parent" we provide a getter. You can see getCompanies() in the code above. If you're only every going to have 1 company per address, then simply have that getter return the 1 company instead of a Set. The inverse is true inside Company, we have a getAddresses().
Company Address, for example, looks like this...