mysql - Grails Scaffold - 为 JodaTime 日期时间字段生成 MySQL 列类型

标签 mysql grails grails-orm

我是 Grails 新手,正在使用 Grails 2.1 和 MySQL 5.5 后端来构建一个示例项目来学习。

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

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

为了隔离问题,我创建了一个简单的域类:

import org.joda.time.DateTime

class Project
{
    String name
    DateTime startDate

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

Controller 只是设置脚手架来使用域类。

My 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
}

看看this post有关更多信息,看看是否有帮助。

关于mysql - Grails Scaffold - 为 JodaTime 日期时间字段生成 MySQL 列类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11977386/

相关文章:

grails - 执行程序或withPool构造

tomcat - 在 gant 脚本中使用(多词位)grails 目标

Grails、SSL 和 Controller 操作,当前的建议是什么

php - 来自多个数据库表的Mysql联合

c# - C# 中的 MySQL 查询错误

mysql - MySQL 根据相邻行的值删除行

Grails .findBy 不适用于 redis

php - mysqli 有更多问题。结果中的数值数据

grails - 无法将 addTo 与 hasmany grails 域一起使用

hibernate - 在加载域之前修改gorm映射