GRAILS将ID类型更改为“int”后,我无法再创建对象

时间:2017-03-04 09:46:18

标签: grails gorm create-table

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

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

但是在我创建一些示例记录的bootstrap中,它不再有效。 错误讯息:

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       
    }
}

2 个答案:

答案 0 :(得分:0)

我建议你改变数据库中的表来处理ID为BIGINT(20)。并从域类定义和映射中删除id。 Gorm会为你做剩下的工作。

答案 1 :(得分:0)

我在此链接中找到了问题的解决方案: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

    }

}