I trying to build a dynamic query similar to:
def domain = DomainName
def ids = 1
def domainClass = "$domain" as Class
domainClass.find("from ${domain} as m where m.job = ${ids} ").id
But it’s not working.
If I’m trying this, all is fine:
def domain = DomainName
def ids = 1
DomainName.find("from ${domain} as m where m.job = ${ids} ").id
How can I use dynamic domain class name with find?
The simplest way is to use the
getDomainClassmethod:Note that if you’re trying to get a single instance by id, use
get:and if you want to get multiple instances and you have a list of ids, you can use getAll
Also it’s a REALLY bad idea to use GStrings with property values embedded – Google ‘SQL Injection’
For example to find a person by username: