Grails 中的 MappedSuperclass 替代方案 [英] MappedSuperclass Alternatives in Grails

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

问题描述

在过去的许多项目中,我使用了this JPA/Hibernate 方法来向系统添加审计功能.它非常有效且不引人注目.

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

是否有 Grails @MappedSuperclass 替代方案(缺少用 Java 而不是 Groovy 编码域模型对象)?如何在没有为其创建表的情况下在每个子类的表方法中声明父类?我已经阅读了 GORM 文档(5.2.3 GORM 中的继承)但除了每个层次表与每个子类表的讨论之外,我没有找到有关如何执行此操作的任何详细信息.

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.

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

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

推荐答案

本质上,就像将 MappedSuperclass 声明为 abstract 一样简单,Grails 不会为它.我意识到 re- 阅读手册:

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:

GORM 支持从抽象基类和具体持久 GORM 实体的继承.IE.具体类是持久的,所以抽象类不是.付费阅读更仔细.

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.

例如

abstract class Auditable {
    Date dateCreated
    Date lastUpdated
}

class Book extends Auditable {
    String title
    String description
}

只会创建 book 表,并且它会有 date_createdlast_updated 列.此外,作为额外的奖励,dateCreatedlastUpdated 属性是 自动时间戳,由 Grails 提供.

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.

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

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