简体   繁体   English

Grails域类继承:配置类列

[英]Grails Domain Class Inheritance: Configure Class Column

I use inheritance in Domain Classes in my Grails app. 我在Grails应用程序的域类中使用继承。 Obviously, inheritance adds a "class" column to my database. 显然,继承为我的数据库添加了一个“类”列。 The content of the "class" column is the fully qualified name of the Domain Class, eg com.myapp.MyClass . “类”列的内容是域类的完全限定名称,例如com.myapp.MyClass

Now if I ever so some refactoring and the class name is no longer com.myapp.MyClass but eg com.myapp.mypackage.MyClass , then the database still contains the old class name which now no longer exists in the app. 现在,如果我进行了一些重构,并且类名不再是com.myapp.MyClass而是例如com.myapp.mypackage.MyClass ,那么数据库仍然包含旧的类名,该类名现在在应用程序中不再存在。

Is there any way to configure the string that is put in the "class" column? 有什么方法可以配置“ class”列中的字符串? Like another unique identifier for the class which is then mapped to the class name in my Grails config or something like this? 就像该类的另一个唯一标识符,然后将其映射到我的Grails配置中的类名或类似的东西?

I think what you need is discriminator for the class. 我认为您所需要的是该课程的鉴别器。

By default when mapping inheritance Grails uses a single-table model where all classes share the same table. 默认情况下,在映射继承时,Grails使用单表模型,其中所有类共享同一张表。 A discriminator column is used to determine the type for each row, by default the full class name. 区分符列用于确定每一行的类型,默认情况下为完整的类名。 ref REF

You can map it like this: 您可以这样映射它:

class PodCast extends Content {
    …
    static mapping = {
        discriminator "audio"
    }
}

Look this documentation it gives you more options to customize it in more details 查看此文档,它为您提供了更多选项,可对其进行更详细的自定义

http://grails.org/doc/latest/ref/Database%20Mapping/discriminator.html http://grails.org/doc/latest/ref/Database%20Mapping/discriminator.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM