4

I have two (5 in fact) domain classes, ClassA and ClassB and have to execute the same query on both

ClassA.where { a == b }.list()

and

ClassB.where { a == b }.list()

I want to write a service class to execute these queries passing the Class object instead of creating a service for each class. I tried this solution

def clazz = grailsApplication.getDomainClass(domainClass)
clazz.where { a == b }.list()

but I have an exception telling me that DefaultGrailsDomainClass dont have the 'where' method.

Is there other way to do this? something like "ClassA.grailsClass.where {}"

Thanks

Community
  • 1
  • 1
Alejandro Vera
  • 377
  • 2
  • 12

1 Answers1

4

The return value of getDomainClass is a GrailsDomainClass / DefaultGrailsDomainClass. Call its getClazz method to get the Class it wraps:

def clazz = grailsApplication.getDomainClass(domainClass).clazz
clazz.where { a == b }.list()
Burt Beckwith
  • 75,342
  • 5
  • 143
  • 156
  • I'm trying to do the same thing with the findAll() where query, but it always returns all domain objects. The query is like this: ``clazz.findAll { prop == value }`` I wonder if this is a bug in Grails 2.1.1? – apa64 Jan 13 '13 at 20:16