user2370316
user2370316

Reputation: 11

How to inherit GORM mapping from non domain class?

Permanently I have some tables and some hibernate classes with mapping annotations. And this classes have abstract superclass with mapping annotations also. But in this superclass there is no table association mapping. All tables are identified in the subclasses. I'm trying to migrate this mapping to GORM model. But all strategies: TablePerHierarchy and TablePerSubclass not approach for my case because all tables is created and can't be changed. I created superclass in the 'src/groovy/somepackage/' and want to inherit mapping and constraints from this class to my subclasses in the 'domain' folder. For constraints it works good but for mapping I can't find documentation how to do this. Does anyone have any ideas?

Example.

In the non-domain folder:

absract class A {
  String a
  static mapping = {
    a column: "column_A"
  }
} 

In the domain folder:

class B extends A {
  String b
  static mapping = {
    b column: "column_B"
  }
}

And

class C extends A {
  String c
  static mapping = {
    c column: "column_C"
  }
}

Needs to get two tables with the column 'column_A' in each of them.

Upvotes: 1

Views: 964

Answers (2)

Greg Ratliff
Greg Ratliff

Reputation: 11

This can now be done a bit more straight forwardly:

class B extends A {
  static mapping = {
    includes A.mapping
  }
}

Upvotes: 1

Thomas Farvour
Thomas Farvour

Reputation: 1103

It's possible using the clone and delegate features. Here's what I did:

class B extends A {

static mapping = {
    def copyMapping = A.mapping.clone()
    copyMapping.delegate = delegate
    copyMapping.call()
    }
}

Upvotes: 4

Related Questions