Grails中的Hibernate类型不匹配

时间:2016-09-29 21:39:33

标签: hibernate grails gorm grails-2.0 type-mismatch

我在使用Java 7和Tomcat 7.0.55的Grails 2.5.0应用程序中收到以下错误:

org.hibernate.TypeMismatchException: Provided id of the wrong type for class com.myCompany.ourPackage.Program. Expected: class java.lang.Integer, got class java.lang.Long

从以下代码行:

review.title = Program.read(programId).title

programId不是Long,正如以下的橡皮鸭调试所证明的那样:

println "programId: ${programId}"
println "programId Type: ${programId.class}" 

上述代码的输出:

propId: 527
propId Type: class java.lang.Integer

如果有帮助,这是堆栈跟踪:

errors.GrailsExceptionResolver TypeMismatchException occurred when
processing request: [GET] /myApp/myController/myAction - parameters:
_: 1475184570336
Provided id of the wrong type for class com.myCompany.ourPackage.Program.
Expected: class java.lang.Integer, got class java.lang.Long. Stacktrace follows:
org.hibernate.TypeMismatchException: Provided id of the wrong type for class com.myCompany.ourPackage.Program. Expected: class java.lang.Integer, got class java.lang.Long
at com.myCompany.myService$_myServiceMethod_closure4$$EPyEG2zw.doCall(myService.groovy:150)
at com.myCompany.myService$$EPyEG2zw.myServiceMethod(myService.groovy:121)
at com.myCompany.myController.myMethod(myController.groovy:92)
at grails.plugin.cache.web.filter.PageFragmentCachingFilter.doFilter(PageFragmentCachingFilter.java:198)
at grails.plugin.cache.web.filter.AbstractFilter.doFilter(AbstractFilter.java:63)
at net.bull.javamelody.JspWrapper.invoke(JspWrapper.java:150)
at net.bull.javamelody.JdbcWrapper$DelegatingInvocationHandler.invoke(JdbcWrapper.java:285)
at net.bull.javamelody.MonitoringFilter.doFilter(MonitoringFilter.java:198)
at net.bull.javamelody.MonitoringFilter.doFilter(MonitoringFilter.java:176)
at grails.plugin.springsecurity.web.filter.GrailsAnonymousAuthenticationFilter.doFilter(GrailsAnonymousAuthenticationFilter.java:53)
at grails.plugin.springsecurity.web.authentication.logout.MutableLogoutFilter.doFilter(MutableLogoutFilter.java:62)
at grails.plugin.springsecurity.web.SecurityRequestHolderFilter.doFilter(SecurityRequestHolderFilter.java:59)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)

应该注意,程序是插件中的域。

班级实施:

package com.myCompany.ourPackage

class Program {

    Integer program

    static mapping = {
        datasource '<datasource>'
        id name: "program", generator: "assigned"
        version false
        cache usage:'read-only'
        table name:'program'
        program column: 'program', sqlType: 'int'
    }
}

其中program是我用programId抓取的数字。

Hibernate / GORM似乎将programId转换为Long。我对么?如果是这样,如何避免呢?

谢谢!

0 个答案:

没有答案