Search code examples
hibernategrailsgrails-orm

How to achieve Hibernate @Any in Grails?


Does Grails somehow support Hibernates @Any annotation? I would like to know if and how.

I can achieve @Any by doing something like:

class A {
  String entityClass
  Long entityId // yes, only support type Long id's

  // dynamically load class (e.g. B), then call B.get(entityId)
}

But I'd like to do it more elegantly.

Eyal Lupu's blog about Hibernate @Any


Solution

  • Since GORM is build on top of Hibernate, you can create a java class with the Hibernate annotations that will be mapped as a domain class.

    There's a section in the docs that cover this integration. Basically you need the configuration xml for hibernate (hibernate.cfg.xml) and create your class under ./src/java.

    Also, the dynamic GORM methods like findAll(), get(), list() and so on, will be available to your classes.