简体   繁体   English

如何在 grails 域类中设置默认值

[英]How can i set default value in grails domain class

Is there any way to set a default value to domain class property?有没有办法为域类属性设置默认值? I have a class called PayMethod , where I want the name property to default to "Cash" and I want this default value when I create this table, is this possible using Constraints?我有一个名为PayMethod的类,我希望name属性默认为"Cash"并且在创建此表时希望使用此默认值,这是否可以使用约束?

package abc

import util.UserUtil
import embed.AuditUser

class PayMethod {

    String name = "Cash"

    AuditUser audit = new AuditUser()
    static embedded = ['audit']    

    static constraints = {
        name blank: false, size: 5..30, unique: true
    }

    static mapping = {
        table 't01i0010'
        id column: 'F_ID', precision: 4, scale: 0
        name column: 'F_NAME', length: 30, defaultValue: 'Cash'
        version column: 'F_REVISION'
    }

    def authUserService
    int insertIndex = 0
    int updateIndex = 0
    static transients = ['authUserService', 'insertIndex', 'updateIndex']    

    def beforeInsert = {
        audit.entryUser = UserUtil.user()
        audit.entryDate = new Date();
    }

    def beforeUpdate = {
        audit.reviseUser = UserUtil.user()
        audit.reviseDate = new Date();
    }

    def afterInsert = {
        if(insertIndex == 0){
            def user = audit.entryUser
            def date = audit.entryDate
            log.info "POST INSERT => ENTERER: ${user} ENTERED: ${date}"
        }
        insertIndex++
    }

    def afterUpdate = {
        if(updateIndex == 0){
            def user = audit.reviseUser
            def date = audit.reviseDate
            log.info "POST UPDATE => REVISE: ${user} REVISED: ${date}"
        }
        updateIndex++
    }
}

This will be possible in 2.2 which should be released this week or next.这将在本周或下周发布的 2.2 中成为可能。 See http://jira.grails.org/browse/GRAILS-5520 for the relevant feature request.有关相关功能请求,请参阅http://jira.grails.org/browse/GRAILS-5520 The syntax will be语法将是

static mapping = {
   name defaultValue: "'Cash'"
}

For now you'll need to do what you're doing - set the value as the default value of the field.现在你需要做你正在做的事情 - 将该值设置为该字段的默认值。 You can manually update the database schema, or do the work as part of a migration.您可以手动更新数据库架构,或将工作作为迁移的一部分进行。

To build on the previous answer, you can use the defaultValue attribute in Grails 2.2 but you need to be careful to put double and single quotes around default values for String properties and double quotes around integer properties so that the default values appear correctly in the DDL.为了建立在前面的答案的基础上,您可以使用 Grails 2.2 中的 defaultValue 属性,但您需要小心地将双引号和单引号放在字符串属性的默认值周围,并将双引号放在整数属性周围,以便默认值正确显示在 DDL 中. So, for instance, you need to use:因此,例如,您需要使用:

static mapping = {
   myStringProperty defaultValue: "'Cash'"
   myIntProperty defaultValue: "0"
}

If you only use single quotes, you will end up with an error like "Column "CASH" not found" Also, as far as I can tell, default values do not work for properties that are enums.如果您只使用单引号,最终会出现类似“未找到列“现金”的错误”此外,据我所知,默认值不适用于枚举属性。

Had the same issue and using static mapping didn't work for me either (using 2.2.3);有同样的问题,使用静态映射对我也不起作用(使用 2.2.3); the below link provided me a functional answer (set the default value in your object declarations):下面的链接为我提供了一个功能性答案(在对象声明中设置默认值):

http://grails.1312388.n4.nabble.com/How-to-set-a-default-value-for-column-td1383753.html http://grails.1312388.n4.nabble.com/How-to-set-a-default-value-for-column-td1383753.html

For String, encapsulate with quotes;对于String,用引号封装; int/integer should just be the value. int/integer 应该只是值。

Hope this helps!希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM