Grails:如何在sql数据库中保存java.sql.Time类型

时间:2015-01-16 13:00:07

标签: sql-server grails gorm datetime-format

我在我的表中添加了一个名为config的新时间类型列,该列由Grails在我的数据库中生成日期时间类型。

这是我的域类

import java.sql.Time
class Config {
      int nbr_delay
      Time max_duration
      static constraints = {
      }
}

在我的GSP页面

<g:field type="time" name="max_duration" value="${fieldValue(bean: configInstance, field: 'max_duration')}" />

现在,当我尝试更改max_duration的值并将其保存在数据库中时,它会显示此错误Failed to convert property value of type java.lang.String to required type java.sql.Time for property max_duration; nested exception is java.lang.IllegalArgumentException: Could not parse date: Unparseable date: "00:30"

我的控制器

configInstance.properties = params
configInstance.save(flush: true)

我搜索了如何解析或操纵这种数据类型,但我没有找到任何解决方案。

1 个答案:

答案 0 :(得分:0)

我要说,如果您有duration字段,请使用intlong来保存分钟或毫秒:

import java.sql.Time
class Config {

  long maxDuration

  void setMax_duration( Time time ){ maxDuration = time.time }

  Time getMax_duration(){ new Time( maxDuration ) }

  static transients = [ 'max_duration' ]
}

然后你可以在你的GSP中做到:

<g:formatDate format="HH:mm" date="${new Date( configInstance.maxDuration )}"/>