gpt4 book ai didi

grails - Grails GORM继承最佳实践

转载 作者:行者123 更新时间:2023-12-02 13:51:34 24 4
gpt4 key购买 nike

我正在一个grails项目中,该项目可能包含36个 Realm 类,每个 Realm 类都具有十几个独特的属性以及一些可以从基本域类继承的共享属性。问题是,grails将生成1个表,其中包含从基类继承的所有域类的所有属性。这意味着一个包含300多个列的表在多个级别上似乎都是有问题的。另一种方法是取消继承,每个域代表一个唯一的数据库表。无论如何,要么将花费大量时间来复制代码,要么尝试管理生成的数据库。我还有其他选择吗?

欢迎所有想法和意见。

最佳答案

似乎您缺少域类的所有重要的tablePerHierarchy映射值。我强烈建议您阅读有关此内容的Inheritance Strategies文档。

从文档中:

By default GORM classes use table-per-hierarchy inheritance mapping. This has the disadvantage that columns cannot have a NOT-NULL constraint applied to them at the database level. If you would prefer to use a table-per-subclass inheritance strategy you can do so as follows:


class Payment {
Integer amount
static mapping = {
tablePerHierarchy false
}
}

class CreditCardPayment extends Payment {
String cardNumber
}

The mapping of the root Payment class specifies that it will not be using table-per-hierarchy mapping for all child classes.

关于grails - Grails GORM继承最佳实践,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25829508/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com