Вопрос

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.

Это было полезно?

Решение

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.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top