【问题标题】:Grails/GORM BigInteger Unsigned RejectedGrails/GORM BigInteger 无符号被拒绝
【发布时间】: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


【解决方案1】:

Long.MAX_VALUE 为 9,223,372,036,854,775,807。你的号码太大了。使用 BigDecimal 应该可以解决这个问题。

BigDecimal fileNameLookup

【讨论】:

  • 我尝试了您的建议,但仍然出现错误。我不确定为什么必须将 bigint(20) 保存为小数。 grails/gorm 中没有 bigint 类型吗?
  • 我继续尝试了一遍,这次我添加了 sqlType: 'decimal(20,0)' 并且成功了。我不确定我是否喜欢这种将其视为数字与将其视为查询中 where 子句中使用的整数的方法。无论平台如何,我最终都想使用未签名的 bigint(20)。
猜你喜欢
  • 1970-01-01
  • 2015-11-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多