【问题标题】:GRAILS After changing type of ID to 'int' I can not create objects any longerGRAILS 将 ID 类型更改为“int”后,我无法再创建对象
【发布时间】:2017-03-04 09:46:18
【问题描述】:

在我们的遗留系统 (MSSQL) 中,我们使用 int 作为 id 的类型,我不想干扰它,所以我像这样更改类型:

static mapping  = {
    id              column: "id",              type:        'int'

但是在我创建一些示例记录的引导程序中,它不再起作用。 错误信息:

Configuring Spring Security UI ...
... finished configuring Spring Security UI

2017-03-04 10:31:20.381 ERROR --- [           main] o.h.p.access.spi.SetterMethodImpl        : HHH000123: IllegalArgumentException in class: com.buffer.ProdBuffer, setter method of property: id
2017-03-04 10:31:20.385 ERROR --- [           main] o.h.p.access.spi.SetterMethodImpl        : HHH000091: Expected type: java.lang.Long, actual value: java.lang.Integer
2017-03-04 10:31:20.419 ERROR --- [           main] o.s.boot.SpringApplication               : Application startup failed

org.springframework.orm.hibernate5.HibernateSystemException: IllegalArgumentException occurred while calling setter for property [com.buffer.ProdBuffer.id (expected type = java.lang.Long)]; target = [com.buffer.ProdBuffer : (unsaved)], property value = [1] setter of com.buffer.ProdBuffer.id; nested exception is IllegalArgumentException occurred while calling setter for property [com.buffer.ProdBuffer.id (expected type = java.lang.Long)]; target = [com.buffer.ProdBuffer : (unsaved)], property value = [1]
    at org.springframework.orm.hibernate5.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:296)
    at org.grails.orm.hibernate.GrailsHibernateTemplate.convertHibernateAccessException(GrailsHibernateTemplate.java:661)
    at org.grails.orm.hibernate.GrailsHibernateTemplate.doExecute(GrailsHibernateTemplate.java:247)
    at org.grails.orm.hibernate.GrailsHibernateTemplate.execute(GrailsHibernateTemplate.java:187)

看起来我错过了什么……除了更改“静态映射”中的类型之外,我还需要做些什么吗?

类的一部分:

class ProdBuffer {
     String status
    String product
    String length
    int    productNo

    static mapping  = {
        table 'LOBuffertv2'
        id              column: "id",              type:        'integer'
        product     column: "Produkt",         sqltype: "char", length: 150
        length      column: "Length",          sqltype: "char", length: 25
        productNo       column: "ProductNo"
    }
    static constraints = {
        status(inList:["Preliminary","Activ","Finished","Cancelled"])
        status                  nullable: true
        product                 nullable: true
        length                  nullable: true
        productNo               nullable: true       
    }
}

【问题讨论】:

  • 试试id column: "id", type: 'integer'
  • 阅读我的答案我是如何解决的。

标签: grails grails-orm create-table


【解决方案1】:

我建议您更改数据库中的表以将 id 处理为 BIGINT(20)。并从域类定义和映射中删除 id。 Gorm 将为您完成剩下的工作。

【讨论】:

  • 这并不容易,很多程序都有数千个数据库访问点和数百个使用 id 类型为 int 的表的存储过程。我不敢改变它们。在这种情况下,我正在处理的工作希望更易于管理,但我知道还会有更多。
  • 我尝试过使用 'integer' 而不是 'int' 但它没有改变任何东西。也许我当时做错了什么。我会再试一次。
  • @larand,请显示您的域类的列表示例。
  • 好的,我添加了域类的一部分。我在引导程序中调试并停止了,所以我可以看到在数据库中创建的表结构。这次它更改为“int”,但是当创建和保存执行时,我得到了上面列出的错误。我有另一堂课没有这样的问题。我还跳过了引导程序,看看在从表单创建时是否遇到同样的失败。就是这样。
【解决方案2】:

我在这个链接中找到了问题的解决方法:Mapping ID to another type

我刚刚在类中将 id 显式声明为 int。 正如我在链接中所理解的那样,我什至不需要在映射中声明类型。

class PBuff {
    int id
    String sawMill
    String status
    String product
    String length

    static mapping  = {
          id column: 'id', type: 'integer'  
    }
    static constraints = {
        status(inList:["Preliminary","Activ","Finished","Cancelled"])

        sawMill                 nullable: true
        status                  nullable: true
        product                 nullable: true
        length                  nullable: true

    }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-06-26
    • 2014-08-11
    • 2021-06-11
    • 1970-01-01
    • 1970-01-01
    • 2012-02-09
    • 2019-12-06
    相关资源
    最近更新 更多