Exception in thread "main" org.hibernate.TypeMismatchException: Provided id of the wrong type. Expected: class java.lang.Long, got class java.lang.Integer
at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:86)
at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:878)
at org.hibernate.impl.SessionImpl.load(SessionImpl.java:795)
at org.hibernate.impl.SessionImpl.load(SessionImpl.java:788)
at com.sinmo.hibernate.dao.Test.main(Test.java:21)
hibernate 3.2查询 主要代码如下:
File fileinfo=(File)session.load(File.class, 3);
用main方法测试:出现Exception in thread "main" org.hibernate.TypeMismatchException: Provided id of the wrong type. Expected: class java.lang.Long, got class java.lang.Integer 错误。
oracle 10g 数据库中主键的类型是number
错误原因:
session.load(File.class,3);方法中的参数3为Int类型,会被自动转换为Integer,而File类中的FIleId属性是Long,它对应的配置文件中也是java.lang.Long,所以出现java.lang.Integer cannot be cast to java.lang.Long异常。
解决方法:
代码改成File fileinfo=(File)session.load(File.class, 3L);在参数中给3加L,表示它是long类型,会自动转换成Long,或者File fileinfo=(File)session.load(File.class, new Long(3));
本文讨论了在使用Hibernate进行数据库查询时遇到的TypeMismatchException错误问题,特别是当查询参数与数据库字段类型不匹配时的情况。通过案例分析,解释了错误原因,并提供了有效的解决方法,包括修改查询参数类型和代码实现细节。
988

被折叠的 条评论
为什么被折叠?



