Alejandro Vera
Alejandro Vera

Reputation: 377

grails: Passing a grails domain class as a function argument

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

Upvotes: 4

Views: 1275

Answers (1)

Burt Beckwith
Burt Beckwith

Reputation: 75681

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

Upvotes: 4

Related Questions