How to set default value for domain class values ​​in Grails 2.2?

In my Grails domain class, I want to set the default values ​​that are stored in the database. I am using mysql as a 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 ​​in the database. What do I need to change to set the default values ​​correctly?

+6
source share
3 answers

If you are on Grails 2.2 above, you can use defaultValue. Look at Bert's answer here. Try, hopefully, this helps:

 Class A { Long someValue Long someOtherValue Boolean someBool Boolean someOtherBool static mapping = { someOtherValue defaultValue: 1 someOtherBool defaultValue: true ... } } 
+6
source

I found that for defaultValue to work with String properties I needed to put double quotes around single quotes, and for defaultValue to work with numeric properties, I had to put double quotes around a number, or the default values ​​would not appear in DDL. For example:

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

Also, as far as I can tell, the default values ​​do not work for the properties that are listed.

+2
source
 class A { long someValue long someOtherValue boolean someBool = Boolean.TRUE boolean someOtherBool = Boolean.TRUE static mapping = { someValue defaultValue: '1' someOtherValue defaultValue: '1' } } 

This will work, verified in 2.2.3.

+2
source

Source: https://habr.com/ru/post/944807/


All Articles