1

I have 4 Grails domain classes ( Domain1, Domain2, Domain3, Domain4). These domain objects map to legacy tables with exactly same set of columns. To be more precise, all of these tables have personFirstName, personLastName and personPhoneNumber columns.

The only thing different between these 4 domain classes is the table name they refer to.

Instead of copy/pasting same piece of code in four different places and just modifying this:

 static mapping = {

       table name:"legacy_table_name_A"     <====== (only difference)

       firstName column: "personFirstName"
       lastName column: "personLastName"
       phoneNumber column:"personPhoneNumber"
 }

I was wondering if there is a way to do this all in one abstract class called MyAbstractDomainClass and have my Domain1, Domain2... extend it like this:

 class MyAbstractDomainClass {
        String firstName
        String lastName
        String phoneNumber

       static mapping = {
           firstName column: "personFirstName"
           lastName column: "personLastName"
           phoneNumber column:"personPhoneNumber"
      }
}

  class Domain1 extends MyAbstractDomainClass {

       static mapping = {

       }
  }

How do I implement the static mapping for each Domain classes to have a different table name?

I am using Grails 2.4.2

Stealth
  • 1,559
  • 4
  • 16
  • 34

0 Answers0