Search code examples
grailssavegrails-ormdefault-value

Is there a way to use the mapping GORM 6 "defaultValue" when saving a domain instance


Suppose I have the following domain class:

class DomainClass{

   String field1
   ...
   static mapping = {
      field1 defaultValue: "'Field1'" 
      ...               
   }

}

Now I want to save the DomainClass instance using the "defaultValue", in the same way when using a sql insert into a postgresql (INSERT INTO domain_class VALUES(default, field2, field3, ...)), e.g.

DomainClass dc = new DomainClass()
dc.field1 = "defaultValue"
...
dc.save()

Solution

  • You can do this...

    class DomainClass{
    
        String field1 = "some default value"
    
        // ...
    }