Search code examples
hibernategrailsinheritancepolymorphismgrails-orm

abstract class in GORM polymorphism


i'm working on a Grails project, Grails version : 2.2.0. i have these classes :

public abstract class A {}
public class B extends A {}
public class C extends A {}

For now, everything is OK class A is in : src/groovy so in DB i have two tables B & C. now i add a new class :

public class D{
A aa
}

now that no longer works because hibernate don't cognize the Type A. One solution is to omit the abstract term but that allow instantiation of A object wich is not so right ! Does any one have an idea about how fix this probleme ? thank you


Solution

  • The problem is this: GORM/Hibernate must be able to instantiate the domain class properties (except for the transient ones, of course). Because of this, a property cannot be of an abstract type. Interfaces don't work for the same reason.

    To keep inheritance, unfortunately you'll have to do that not so right thing: drop the abstract and make A a domain class.