将生成的数据库列映射到Grails域类属性

将生成的数据库列映射到Grails域类属性,grails,gorm,Grails,Gorm,在我的Grails 2.5.X应用程序中,我有一个如下所示的域类: class FormData { String submittedFields Boolean submitted static constraints = { submittedFields nullable: true } static mapping = { // can I do something here to map su

在我的Grails 2.5.X应用程序中,我有一个如下所示的域类:

class FormData {

    String submittedFields
    Boolean submitted

    static constraints = {
        submittedFields nullable: true
    }

    static mapping = {        
        // can I do something here to map submitted to a generated
        // column of the form_data table
    }
}
我想将
submitted
属性映射到
form_data
表的生成列,即SQL语句将创建的列

alter table form_data add submitted tinyint 
GENERATED ALWAYS AS (if(submitted_fields is null,0,1));
具体来说,当我从域模型创建架构时,应该创建这个生成的列,例如通过运行
schema export
脚本


submitted
映射到生成的列的结果是,相应的域类属性应该是只读的,或者至少,为其赋值应该没有效果。

如果只想在数据库端处理列的值,并且不希望从grails/hibernate端插入或更新它。您可以将列设置为可插入:false updatetable:false

static mapping = {
 submitted insertable:false, updateable:false
}

现在,即使在grails中更改了值,新值也不会在数据库中更新。

感谢您的回复,这非常有帮助,但没有回答我问题的另一部分:如何让grails在生成的模式中包含此列?您是指数据库迁移插件吗?--这应该会生成没有任何问题的列,我指的是Grails生成模式时,例如运行
模式导出
脚本时,或者在
dbCreate=“create drop”
模式下启动应用程序时。就目前情况而言,Grails无法创建此列,因为我还没有在域模型中的任何地方定义它的性质,所谓“性质”,我的意思是
submitted tinyint始终生成为(if(submitted_fields为null,0,1))