MappedSuperclass Grails中的替代品 [英] MappedSuperclass Alternatives in Grails

查看:121
本文介绍了MappedSuperclass Grails中的替代品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在过去的许多项目中,我使用了这个 JPA / Hibernate方法来为系统添加审计功能。这是非常有效和不显眼的。

是否有Grails @MappedSuperclass替代方案(缺少Java编码领域模型对象而不是Groovy)?如何在没有为其创建表的情况下以每个子类的方式声明父类?我已阅读GORM文档( 5.2.3 GORM中的继承),但除了table-per-hierarchy和table-per-subclass的讨论之外,我没有找到关于如何做的细节这个。



或者,在Grails中实现这种类型的审计的推荐方式是什么? 方法

好的,做了一些更多的搜索,我回答了我自己的问题:)

基本上,就像将MappedSuperclass声明为抽象一样简单Grails不会为它创建表格。我通过重新阅读手册(RTFM基本上......创造奇迹):GORM支持从抽象基类和具体持久GORM实体的继承。即具体的类是持久的,所以抽象的不是。

 抽象类Auditable { 
日期日期创建
日期lastUpdated
}

类书籍扩展可审计{
字符串标题
字符串描述
}

只有书籍表格会被创建,并且会有


date_created


last_updated


列。此外,作为额外的奖励,dateCreated和lastUpdated属性是由Grails自动添加时间戳

希望这有助于其他人。

In many past projects, I used this JPA / Hibernate approach to add auditing capabilities to a system. It's very effective and unobtrusive.

Is there a Grails @MappedSuperclass alternative (short of coding domain model objects in Java instead of Groovy)? How can one declare a parent class in a table-per-subclass approach without having a table created for it? I've read the GORM documentation (5.2.3 Inheritance in GORM) but besides the table-per-hierarchy vs. table-per-subclass discussion, I did not find any details on how to do this.

Alternatively, what is the recommended way to achieve this type of auditing in Grails?

解决方案

OK, did a little bit more searching and I answered my own question :)

Essentially, it's as simple as declaring the MappedSuperclass as abstract and grails will not create a table for it. I realized by re-reading the manual (RTFM basically...works wonders): "GORM supports inheritance both from abstract base classes and concrete persistent GORM entities." I.e. concrete classes are persistent, so abstract ones are not. Pays to read more carefully.

E.g.

abstract class Auditable {
    Date dateCreated
    Date lastUpdated
}

class Book extends Auditable {
    String title
    String description
}

Only the book table will be created and it will have the

date_created

and

last_updated

columns. Furthermore, as an added bonus, the dateCreated and lastUpdated properties are auto time-stamped by Grails.

Hope this helps others.

这篇关于MappedSuperclass Grails中的替代品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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