【发布时间】:2014-08-18 15:39:04
【问题描述】:
我收到以下错误:
| Error 2014-08-18 11:25:00,324 [localhost-startStop-1] ERROR context.GrailsContextLoaderListener - Error initializing the application: Validation Error(s) occurred during save():
- Field error in object 'my.package.Content' on field 'fileNameLookup': rejected value [16731516642733300018]; codes [my.package.Content.fileNameLookup.typeMismatch.error,my.package.Content.fileNameLookup.typeMismatch,content.fileNameLookup.typeMismatch.error,content.fileNameLookup.typeMismatch,typeMismatch.my.package.Content.fileNameLookup,typeMismatch.fileNameLookup,typeMismatch.java.lang.Long,typeMismatch]; arguments [fileNameLookup]; default message [Could not convert number [16731516642733300018] of type [java.math.BigInteger] to target class [java.lang.Long]: overflow]
Message: Validation Error(s) occurred during save():
- Field error in object 'my.package.Content' on field 'fileNameLookup': rejected value [16731516642733300018]; codes [my.package.Content.fileNameLookup.typeMismatch.error,my.package.Content.fileNameLookup.typeMismatch,content.fileNameLookup.typeMismatch.error,content.fileNameLookup.typeMismatch,typeMismatch.my.package.Content.fileNameLookup,typeMismatch.fileNameLookup,typeMismatch.java.lang.Long,typeMismatch]; arguments [fileNameLookup]; default message [Could not convert number [16731516642733300018] of type [java.math.BigInteger] to target class [java.lang.Long]: overflow]
Line | Method
->> 6 | doCall in BootStrap$_closure1
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| 327 | evaluateEnvironmentSpecificBlock in grails.util.Environment
| 320 | executeForEnvironment . . . . . in ''
| 296 | executeForCurrentEnvironment in ''
| 266 | run . . . . . . . . . . . . . . in java.util.concurrent.FutureTask
| 1142 | runWorker in java.util.concurrent.ThreadPoolExecutor
| 617 | run . . . . . . . . . . . . . . in java.util.concurrent.ThreadPoolExecutor$Worker
^ 745 | run in java.lang.Thread
Error |
Forked Grails VM exited with error
领域类:
package my.package
// need to fix constraints
class Content {
// int id
Long fileNameLookup
static mapping = {
version false
fileNameLookup column: 'file_name_lookup', type:org.hibernate.type.LongType, class: Long
}
static constraints = {
fileNameLookup(nullable:true, display:false, editable: false)
}
}
Bootstrap.groovy:
import my.package.Content
class BootStrap {
def init = { servletContext ->
new Content(fileNameLookup:16731516642733300018).save(failOnError:true)
}
def destroy = {
}
}
我尝试了 Long、BigInteger、Integer 等...并为此花费了数小时试图弄清楚如何让这个 bigint(20) 无符号以保存到测试数据库中。我如何告诉 Grails/Gorm 这个数字是一个 bigint(20),这样无论我使用什么数据库,它都能正确处理它?
【问题讨论】:
-
您是否尝试过一个可以放入
Long的号码?正如错误所说,16731516642733300018太大而无法放入Long,所以你会溢出 -
感谢您指出这一点,我想知道正确的类型是什么,以便我可以在数据库中放入该大小的数字。
-
错误消息告诉您
Long fileNameLookup字段太小而无法存储该数字。将类型更改为java.math.BigInteger是否会给您带来不同的错误消息? -
使用 H2 数据库我无法让它工作,我知道它可以与 MySQL 一起工作,因为我用来生成 20 位数字的代码最初是为 MySQL 开发的。我编写了一个 groovy 类来为我处理转换,这样我就可以使用任何数据库,而无需仅依赖 MySQL 的代码。更改数据源后我所做的是将 sqlType: 'bigint(20) unsigned' 添加到映射中。我真的希望让它适用于任何数据库,但是对于 H2 中的任何字段来说,该字段都太长了。
-
有些数据库不支持 unsigned bigint 列类型,H2 就是其中之一。
标签: grails groovy grails-orm