繁体   English   中英

Grails Scaffold-为JodaTime DateTime字段生成的MySQL列类型

[英]Grails Scaffold - Generated MySQL Column Type for JodaTime DateTime field

我是Grails的新手,并且正在将Grails 2.1与MySQL 5.5后端结合使用来构建要学习的示例项目。

我安装了JodaTime 1.4插件,然后运行grails install-joda-time-templates

但是,当我将域类字段声明为org.joda.time.DateTime类型时,尝试保存新条目时出现错误。

为了找出问题,我创建了一个简单的Domain Class:

import org.joda.time.DateTime

class Project
{
    String name
    DateTime startDate

    static constraints = {
        name(blank: false, maxSize: 50)
        startDate(validator: {return (it > new DateTime())})
    }
}

控制器只是将脚手架设置为使用域类。

我的DataSource.groovy指定dbCreate = "create-drop" ,因为我让Grails创建表。

这是我尝试保存时遇到的错误:

Class:com.mysql.jdbc.MysqlDataTruncation
Message:Data truncation: Data too long for column 'start_date' at row 1

当我查看Grails创建的MySQL数据库中的project.start_date列时,类型为TINYBLOB。

我的想法是,TINYBLOB可能不足以存储JodaTime DateTime字段的数据。

有谁知道我怎样才能让Grails创建合适的类型?

非常感谢你。

在您的Config.groovy中:

grails.gorm.default.mapping = {
    "user-type" type: PersistentDateTime, class: DateTime
    "user-type" type: PersistentLocalDate, class: LocalDate
}

和您的映射关闭:

static mapping = {
    startDate type: PersistentDateTime
}

看看这篇文章以获取更多信息,看看是否有帮助。

我做了什么使它起作用(Grails 2.1):

1)添加到buildConfig中:

compile "org.jadira.usertype:usertype.jodatime:1.9"

2)刷新依赖项

3)运行此命令以添加受支持的用户类型:

grails install-joda-time-gorm-mappings

4)最后在域类中:

import org.jadira.usertype.dateandtime.joda.*

static mapping = {
 column_name type: PersistentDateTime
}

在此处找到文档: 持久性

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM