How to set default value for Domain Class Values in Grails 2.2?

confile picture confile · May 12, 2013 · Viewed 13.9k times · Source

In my Grails domain class I want to set default values which do persist in the database. I use mysql as database. I tried to do this:

class A {

   long someValue = 1
   long someOtherValue
   boolean someBool = true
   boolean someOtherBool

   static mapping = {
      someOtherValue defaultValue: 1
      someOtherBool defaultValue: true  
   }
}

But nothing works. There are no default values set in the database. What do I have to change to get my default values being set correctly?

Answer

Alidad picture Alidad · May 12, 2013

If you are on Grails 2.2 above then you can use defaultValue. Look at Burt's answer here Try it, hope this helps:

Class A {
      Long someValue 
      Long someOtherValue

      Boolean someBool
      Boolean someOtherBool

     static mapping = {
        someOtherValue defaultValue: 1
        someOtherBool  defaultValue: true  
        ...
     } 

}