How to inherit GORM mapping from non domain class?

假如想象 提交于 2020-01-05 10:32:09

问题


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.


回答1:


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()
    }
}



回答2:


This can now be done a bit more straight forwardly:

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


来源:https://stackoverflow.com/questions/16484723/how-to-inherit-gorm-mapping-from-non-domain-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!