我是 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/