Grails 中的 MappedSuperclass 替代方案

发布于 2024-07-09 15:36:19 字数 635 浏览 6 评论 0 原文

在过去的许多项目中,我使用 这种 JPA/Hibernate 方法可以向系统添加审核功能。 它非常有效且不引人注目。

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

或者,在 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

圈圈圆圆圈圈 2024-07-16 15:36:19

本质上,就像将 MappedSuperclass 声明为 abstract 一样简单,Grails 不会为它创建表。 我通过 re 实现-阅读手册

GORM 支持从抽象基类和具体持久 GORM 实体继承。 即,具体类是持久的,因此抽象类不是。 仔细阅读是值得的。

例如,

abstract class Auditable {
    Date dateCreated
    Date lastUpdated
}

class Book extends Auditable {
    String title
    String description
}

只会创建图书表,并且它将具有 date_createdlast_updated 列。 此外,作为额外的好处,dateCreatedlastUpdated 属性是 Grails 的自动时间戳

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 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文