Setting default value for Date field in Grails Domain Class
Asked Answered
O

3

8

I'm trying to set a default value for a Date field in a Domain class.

I can use defaultValue in the mapping configuration but it doesn't work with Date fields (I've tried it on String and Integer and it works fine).

This is an example:

class Something {

    Date myField

    static mapping = {
        myField defaultValue: new Date()
    }

}

This code fails because the CREATE statement that Hibernate generates is incorrect. It is something like:

... my_field datetime default Mon Nov 25 17:59:08 UYST 2013 not null ...
Ollayos answered 25/11, 2013 at 20:31 Comment(0)
H
15

You can always initialize the field in the static initializer or set the value in the constructor:

class Something {
    // initializer
    Date myField = new Date()

    // or in the ctor
    Something() {
        myField = new Date()
    }
}

This doesn't set a default value in the database schema, it merely sets the value of the field on creation of the instance. If you want the schema to have a default value, you can use the 'defaultValue' mapping entry like so:

class Something {
    Date myField

    static mapping = {
        myField defaultValue: "now()"
    }
}

the value you set for the default value is dependent on your database vendor. (notice the use of sql now() method rather than Java/Groovy new Date().

Herschel answered 25/11, 2013 at 20:40 Comment(1)
Thanks! I supposed that was the solution, but didn't want a database engine specific function like now()Ollayos
P
2

GORM readily caters for the most basic Date use cases; create and update.

Simply include keywords dateCreated and lastUpdated into the domain's properties for the default functionality to occur.

Warning: If their constraints are nullable: false this will cause a fail. Either remove those constraints or set autoTimestamp to false.

For example:

class MyDomain {
   Date dateCreated
   Date lastUpdated
   Date yesterday = new Date().previous()
   Date weekAgo = new Date() - 7
   Date monthAgo = new Date() - 30
   Date usaIndepenceDay = new Date().copyWith(
         year: 1776, 
         month: Calendar.JULY, 
         dayOfMonth: 4, 
         hourOfDay: 0,
         minute: 0,
         second: 0)

   static mapping = {
     //autoTimestamp false
   }

   static constraints = {
     //dateCreated nullable: false
   }
}

Read the more about groovy dates at this SO answer, the groovy date api, and GORM's date-event features here

Pluto answered 29/6, 2016 at 13:14 Comment(3)
Thanks, I already knew about the dateCreated and lastUpdated fields, but those had nothing to do with my original question :DOllayos
I loosely threw them in there because I couldn't find a good and fast answer on grails dates, and adds something easy to digestPluto
How would I set the default date to tomorrows date? Or say four days from now?Ane
T
2

You can use this for default crated date auto get from system date

class User {
String userName
String firstName
String lastName
Date createdDate = new Date() // set system date


static mapping = {
    version false
    id generator: 'increment'
    cache true
}

static constraints = {
    userName(unique: true)
}
}
Treva answered 5/10, 2016 at 10:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.