简体   繁体   English

grails - 域类中的继承 - 将列继承到基表中

[英]grails - inheritance in domain class - inherited columns into base tables

I have a Record class as:我有一个记录类:

class Record {
    Date dateCreated
    Date lastUpdated
    def beforeInsert() {
        dateCreated = new Date()
    }
    def beforeUpdate() {
        lastUpdated = new Date()
    }
    static mapping = { tablePerHierarchy false }
}

And this class is inherited by several other domain classes - for example:并且这个类被其他几个领域类继承——例如:

class User extends Record{
    String userName
    String password
    String email
}

My question here is: Is there any way to embedd the columns of Record table into the table which extends it?我的问题是:有没有办法将 Record 表的列嵌入到扩展它的表中?

You can define tablePerClass or tablePerHierarchy strategies.您可以定义tablePerClasstablePerHierarchy策略。 See documentation here . 请参阅此处的文档

So, following, your code you should use:因此,接下来,您应该使用的代码:

static mapping = {
    tablePerHierarchy true
}    

You could use inheritance here, however that might restrict your class hierarchy.您可以在此处使用继承,但这可能会限制您的类层次结构。 I'm not as familiar with GORM as i am with straight hibernate (which is what GORM uses).我对 GORM 的熟悉程度不如直接休眠(GORM 使用的是这种模式)。 If you find you want to use composition instead of inheritance, i believe something like this would work (i haven't tried it though):如果你发现你想使用组合而不是继承,我相信这样的事情会起作用(虽然我还没有尝试过):

@Embeddable class AuditTimestamps { ... }

class User {
   @Embedded AuditTimestamps auditTimestamps

   def beforeInsert() { auditTimestamps.beforeInsert() }
   def beforeUpdate() { auditTimestamps.beforeUpdate() }

}

I've done something exactly like this is straight java/hibernate and it works like a charm.我已经做了一些完全像这样的事情,这是直接的 java/hibernate,它就像一个魅力。

First thing is first, You don't need a brilliance here if your problem is dataCreated and dateUpdated, You could just put those fields where ever in the domain and GORM magically fills them out!首先,如果您的问题是 dataCreated 和 dateUpdated,那么您不需要在此处表现出色,您可以将这些字段放在域中的任何位置,GORM 会神奇地填写它们! If your plan is to not add them everywhere, you could simply create a Groovy class in src/groovy directory and then inherit those!如果您的计划是不在任何地方添加它们,您可以简单地在src/groovy目录中创建一个 Groovy 类,然后继承它们!

If you have a complex inheritance tree, then injecting those properties dynamical could be a solution too!如果你有一个复杂的继承树,那么动态注入这些属性也是一种解决方案! You can do this in bootstrap!您可以在引导程序中执行此操作!

def grailsApplication // Autowiring

grailsApplication.domainClasses.each { domainClass -> 
    // add the fields you want to the metaclass
}

If you plan to temper with the data before update and insert then override the beforeInsert and beforeUpdate methods.如果您计划在更新和插入之前调整数据,则覆盖beforeInsertbeforeUpdate方法。 The drawback of overriding those methods from metaClass is that you cannot use them in the actual domain Classes.从 metaClass 覆盖这些方法的缺点是您不能在实际的域类中使用它们。

The third way to do this is use custom eventListners and handling GORM(Hibernate) events.For details you should checkout here , the author is demonstrating how to do it from a plugin.第三种方法是使用自定义 eventListners 和处理 GORM(Hibernate) 事件。有关详细信息,您应该在此处查看,作者正在演示如何从插件中执行此操作。 Am sure you can map it to suit your use.确保您可以映射它以适合您的使用。

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

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