1

I have been using the Joda-Time plugin for Grails. I'm really starting to love it but, I'm trying to set a default value for domain attribute (Period type:PersistentPeriod). Everything is working great. I can save and retrieve Joda-Time data, the tag library is working well, calculations and conversions are flawless but, everything blows up when I try to compile after setting a default value for a Joda-Time attributes. Example:

class Person { 
    DateTime date 
    Period totalTime 
    static mapping = {
        totalTime (type: PersistentPeriod, defaultValue:Period.ZERO)
        date (type: PersistentDateTime, defaultValue:DateTime.now())
    } 
}

Is there a way to do this with default values or should I keep the Joda-Time attributes nullable and blank.

dmahapatro
  • 49,365
  • 7
  • 88
  • 117
CheddarMonkey
  • 449
  • 5
  • 12
  • What actually blows up ? Please provide a stack trace or error description. – rxn1d Apr 03 '14 at 07:19
  • SchemaExport Unsuccessful: SchemaExport Column "PT0S" not found; SQL statement: SchemaExport Table "ACCT_SHIFT" not found; SQL statement: – CheddarMonkey Apr 04 '14 at 15:54

1 Answers1

2

Specifying "defaultValue" won't work in the way you are trying to use it. It is meant to be used as a default value that MySQL can use when specifying the create table syntax. If you simply want any class that doesn't have the value set to be a default then rewrite your Person class like so:

class Person { 
  DateTime date = DateTime.now() 
  Period totalTime = Period.ZERO
  static mapping = {
    totalTime (type: PersistentPeriod)
    date (type: PersistentDateTime)
  } 
}

Alternatively you could use defaultValue: "CURRENT_TIMESTAMP" for DateTime.now() by MySQL will expect a field type "TIMESTAMP" and not "DATETIME" in order for it to work.

th3morg
  • 4,321
  • 1
  • 32
  • 45
  • That works. I guess I misunderstood the use of defaultValue. Thanks for clearing that up. – CheddarMonkey Apr 04 '14 at 15:51
  • After researching a little more about defaultValue, and considering @th3morg comments, I realized that 'defaultValue' in the mapping closure specifically affects the creation and update of the database. Therefore, the value assigned to defaultValue must correspond to a 'native' data type of the database I am using. So this works: totalTime (type: PersistentPeriod, defaultValue:"PT0S") -- "PT0S" being the actual string Joda-Time stores in the database column, a varchar(255). – CheddarMonkey Apr 04 '14 at 16:27
  • The declaration of a default value in the property, 'Period totalTime = Period.ZERO' is used by GORM when the application saves/updates a domain object and there is no value/null value for the totalTime attribute. [See this Nabble post about defaultValue.](http://grails.1312388.n4.nabble.com/right-way-to-define-default-value-to-a-properties-in-domain-class-td4649809.html) I think I'm understanding this correctly. Please correct me if I'm wrong. – CheddarMonkey Apr 04 '14 at 16:34
  • ... and yet another amendment to this topic. "PT0S" does not work. Not as specifically typed. "'PT0S'" works. – CheddarMonkey Apr 04 '14 at 16:41
  • ... and another. the date in the mapping closure can be set like this: date type: PersistentDateTime, defaultValue: "now()". This also works for the standard Grails Date data type. [See this other StackOverflow question](http://stackoverflow.com/questions/20202702/setting-default-value-for-date-field-in-grails-domain-class) – CheddarMonkey Apr 04 '14 at 17:40