How can i set default value in grails domain class

后端 未结 3 1393
遥遥无期
遥遥无期 2021-02-05 00:27

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 \

3条回答
  •  情歌与酒
    2021-02-05 00:52

    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.

提交回复
热议问题