Grails: passing a grails domain class as an argument to a function

I have two (in fact) domain classes, ClassA and ClassB, and must fulfill the same request on both

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

and

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

I want to write a service class to fulfill these requests passing a 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 does not have a "where" method.

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

thanks

+4
source share
1 answer

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

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

All Articles