Search code examples
hibernategrailsgrails-orm

Refactor Grails Domain Classes with inheritance


I have 3 Objects in my grails application:

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
}

In the database I have 3 tables, one for message with many fields, a table for inbound_message and one for outbound_message. So for this example, the message table has the columns field_a and field_b. The inbound_message table has field_in

Because we have some performance issues with the 3 tables, I want to change this to two tables. So inbound_message should contain all fields from message and the additional fields from InboundMessage.

So there should be only two tables, inbound_message and outbound_message. inbound_message should have the columns field_a, field_b and field_in outbound_message should have the columns field_a, field_b, and field_out

How do I achieve this without changing much in my grails code? It would be perfect to keep the abstract message class, so that I can have the fields for every message in one place. But the end result should be: Only two tables in the database.


Solution

  • I found a solution for this problem:

    In all three classes I added this code:

    static mapping = {       
        tablePerConcreteClass true
    }
    

    Now it is working as I expected