是否可以在grails中动态地映射域对象的表名称? [英] Is it possible to map a table name for a domain object dynamically in grails?
本文介绍了是否可以在grails中动态地映射域对象的表名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个类似于
class Foo {
String name
static mapping = {
table 'foo'
}
}
但我想让它更像:
static mapping = {
table "foo_${dynamicVarThatComesFromRequest}"
}
我想知道的是可能?
谢谢!
推荐答案
您可以添加一个Hibernate拦截器来处理所有SQL语句,并解析/替换您在映射中输入的表名称中的一些令牌,并使用您要使用的实际表名。
It is possible. You can add a Hibernate interceptor to process all SQL statements and parse/replace some token in the table name you enter in the mapping with the actual table name you want to use.
src / groovy / DynamicTableNameInterceptor.groovy:
src/groovy/DynamicTableNameInterceptor.groovy :
import org.hibernate.EmptyInterceptor
public class DynamicTableNameInterceptor extends EmptyInterceptor {
public String onPrepareStatement(String sql) {
// some kind of replacement logic here
def schema=SomeHelperClass.resolveSchema()
return sql.replaceAll('_SCHEMA_', schema)
}
}
grails-app / conf / spring / resources.groovy:
grails-app/conf/spring/resources.groovy:
beans = {
// This is for Grails 1.3.x , in previous versions, the bean name is eventTriggeringInterceptor
entityInterceptor(DynamicTableNameInterceptor)
}
这篇关于是否可以在grails中动态地映射域对象的表名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文