如何禁用每个环境的gorm.mapping id生成

时间:2011-11-15 14:43:14

标签: grails gorm

我在生产中使用oracle db,因为我在gorm中使用id生成器按顺序进行映射。

但是现在我只想在我在文件中使用hsqldb的开发中禁用域的所有映射。

这是为了不必为每个开发者pc安装oracle。

我在BootStrap中试过这个:

import grails.util.GrailsUtil;

class DevBootStrap {

    def grailsApplication

    def init = { servletContext ->
        if(GrailsUtil.environment in ["development"]){
            grailsApplication.domainClasses.each { domainClass ->
                domainClass.metaClass.mapping = null
            }
        }
    }

    def destroy = {

    }

}

但它不起作用。

我也试过

grails.gorm.default.mapping = null

也不起作用。

我想也许是_Events中的东西。但我不知道这件事。

你能给我任何指针吗?

问候

2 个答案:

答案 0 :(得分:0)

我想这可能有用,但是你需要添加.clazz,否则你会在错误的课程中玩。

grailsApplication.domainClasses.each { domainClass ->
    domainClass.clazz.metaClass.mapping = null
}

我不确定,当你更改映射的时间点是否足够早。

答案 1 :(得分:0)

我发现您可以使用Environment.current访问域类映射中的当前环境。因此,在每个域类中,您都可以尝试添加:

static mapping = {
    if ( Environment.current != Environment.DEVELOPMENT ) {
        //define mapping 
    }
}  
相关问题