日期:2014-05-16  浏览次数:20376 次

hibernate 类型错误

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));