使用继承重构 Grails 域类

时间:2021-03-04 13:50:09

标签: hibernate grails gorm

我的 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 代码的情况下实现这一目标?保留抽象消息类将是完美的,这样我就可以在一个地方拥有每条消息的字段。 但最终的结果应该是:数据库中只有两张表。

1 个答案:

答案 0 :(得分:0)

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

在所有三个类中,我都添加了以下代码:

static mapping = {       
    tablePerConcreteClass true
}

现在它按我的预期工作

相关问题