How can i set default value in grails domain class
Asked Answered
R

3

46

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?

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++
    }
}
Rubberneck answered 27/11, 2012 at 4:36 Comment(0)
D
61

This will be possible in 2.2 which should be released this week or next. See http://jira.grails.org/browse/GRAILS-5520 for the relevant feature request. 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.

Disperse answered 27/11, 2012 at 5:28 Comment(2)
Its not working, i want set defaultvalue name='cash' in the database when i create this tableRubberneck
Using defaultValue isn't working? As I said, that will only work in 2.2 which hasn't been released yet. Until then you can manually update the database.Disperse
N
23

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. 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.

Nadabus answered 12/7, 2013 at 18:0 Comment(0)
B
3

Had the same issue and using static mapping didn't work for me either (using 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

For String, encapsulate with quotes; int/integer should just be the value.

Hope this helps!

Bainbrudge answered 15/1, 2014 at 18:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.