【问题标题】:Refactor Grails Domain Classes with inheritance使用继承重构 Grails 域类
【发布时间】:2021-03-04 13:50:09
【问题描述】:

我的 grails 应用程序中有 3 个对象:

abstract class Message {
   //many fields here
    static mapping = {         
        tablePerHierarchy false
    }
    int fieldA
    int fieldB
}
class InboundMessage extends Message {
  //some fields
  int field_in
}
class OutboundMessage extends Message {
  //some fields
  int field_out
}

在数据库中,我有 3 个表,一个用于包含多个字段的消息,一个用于 inbound_message,另一个用于 outbound_message。 因此,对于本示例,消息表具有字段 field_a 和 field_b 列。 inbound_message 表有 field_in

因为这 3 个表存在一些性能问题,所以我想将其更改为两个表。所以 inbound_message 应该包含 message 中的所有字段以及 InboundMessage 中的其他字段。

所以应该只有两个表,inbound_message 和 outbound_message。 inbound_message 应该有 field_a、field_b 和 field_in 列 outbound_message 应包含字段 field_a、field_b 和 field_out 列

如何在不更改 Grails 代码的情况下实现这一目标?保留抽象消息类是完美的,这样我就可以将每条消息的字段放在一个地方。 但最终结果应该是:数据库中只有两个表。

【问题讨论】:

  • “最好保留抽象消息类,这样我就可以将每条消息的字段放在一个地方。” - 你能在共享特征中定义共享字段吗?

标签: hibernate grails grails-orm


【解决方案1】:

我找到了解决这个问题的方法:

在所有三个类中我都添加了这段代码:

static mapping = {       
    tablePerConcreteClass true
}

现在它正在按我的预期工作

【讨论】:

  • 如果你只想要 2 个表,不清楚为什么需要 3 个类。 Message 需要成为一个类而不是一个 trait 是否有某些原因?
  • 我从没听说过traits,来看看
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多