首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > 其他数据库 >

hibernate 类型异常

2012-08-14 
hibernate 类型错误Exception in thread main org.hibernate.TypeMismatchException: Provided id of th

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

热点排行