[英]Grails Scaffold - Generated MySQL Column Type for JodaTime DateTime field
I am new to Grails and am using Grails 2.1 with a MySQL 5.5 backend to build a sample project to learn. 我是Grails的新手,并且正在将Grails 2.1与MySQL 5.5后端结合使用来构建要学习的示例项目。
I installed JodaTime 1.4 Plug-in and then ran grails install-joda-time-templates
我安装了JodaTime 1.4插件,然后运行
grails install-joda-time-templates
However, when I declared a Domain Class field to be of type org.joda.time.DateTime, I got an error when attempting to save a new entry. 但是,当我将域类字段声明为org.joda.time.DateTime类型时,尝试保存新条目时出现错误。
In order to isolate the problem, I created a simple Domain Class: 为了找出问题,我创建了一个简单的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())})
}
}
The controller just sets scaffold to use the Domain Class. 控制器只是将脚手架设置为使用域类。
My DataSource.groovy specifies dbCreate = "create-drop"
, as I am letting the tables get created by Grails. 我的DataSource.groovy指定
dbCreate = "create-drop"
,因为我让Grails创建表。
Here is the error I get when I try to save: 这是我尝试保存时遇到的错误:
Class:com.mysql.jdbc.MysqlDataTruncation
Message:Data truncation: Data too long for column 'start_date' at row 1
When I look at project.start_date column in the MySQL database that Grails created, the type is TINYBLOB. 当我查看Grails创建的MySQL数据库中的project.start_date列时,类型为TINYBLOB。
My thought is that TINYBLOB may not be sufficient to store the data for a JodaTime DateTime field. 我的想法是,TINYBLOB可能不足以存储JodaTime DateTime字段的数据。
Does anyone know how I can make Grails create an appropriate type? 有谁知道我怎样才能让Grails创建合适的类型?
Thank you very much. 非常感谢你。
In your Config.groovy: 在您的Config.groovy中:
grails.gorm.default.mapping = {
"user-type" type: PersistentDateTime, class: DateTime
"user-type" type: PersistentLocalDate, class: LocalDate
}
And your mapping closure: 和您的映射关闭:
static mapping = {
startDate type: PersistentDateTime
}
Take a look at this post for more info, see if it helps. 看看这篇文章以获取更多信息,看看是否有帮助。
What I did to make it work (Grails 2.1): 我做了什么使它起作用(Grails 2.1):
1) add to buildConfig: 1)添加到buildConfig中:
compile "org.jadira.usertype:usertype.jodatime:1.9"
2) refresh the dependencies 2)刷新依赖项
3) run this command to add the user-type supported: 3)运行此命令以添加受支持的用户类型:
grails install-joda-time-gorm-mappings
4) finally in the domain class: 4)最后在域类中:
import org.jadira.usertype.dateandtime.joda.*
static mapping = {
column_name type: PersistentDateTime
}
Documentation was found here: persistence 在此处找到文档: 持久性
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.