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



我在名为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)

我搜索了如何解析或操作此数据类型,但尚未找到任何解决方案。

我会

说,如果你有一个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 )}"/>

最新更新