Grails - 动态分配 table 以进行搜索

Grails - Dynamically assign table to search from

基本上我想做的是接收我的 table 之一的字符串形式的名称,然后从 table 中搜索。到目前为止我尝试过的方法(均无效):

params.tableName.findById(100)

和:

def tables = grailsApplication.domainClasses
def table
tables.each {
    if(it.name.toString() == params.tableName) {
        table = it
    }
}
table.findById(100)

我意识到我的问题的描述有点短,这主要是因为它很难解释,我希望我提供的代码示例能够弥补它,但如果它太模糊, 让我知道,我会尽量说得更清楚。此外,我在 Grails 2.2.5 中工作,因此非常感谢适用于此版本的答案。

我不确定这是否明智,但以下对我有用:

def id = Eval.me( "your.domain.package.${params.tableName}.findById( 100 )" )

最终成功的原因如下:

def table = grailsApplication.getClassForName("path.to.package.${params.tableName}")
def record = table.get(100)
println record // successfully prints the record and as such I'm assuming it works