问题描述
场景: mysql数据库定义数据类型为bigInt, hibernate映射文件中定义类型为Long,通过hibernate查询出来的确实java.math.BigInteger private Long id;private Long projectId;private Long orgRoleId; <id name="id" type="java.lang.Long"> <column name="id" /> <generator class="identity" /> </id> <property name="projectId" type="java.lang.Long"> <column name="projectId" not-null="true" /> </property> <property name="orgRoleId" type="java.lang.Long"> <column name="orgRoleId" not-null="true" /> </property>数据库定义类型为bigInt, Mysql数据库查询:List<Map<String, Object>> list = this.hibernateTemplate.execute(new HibernateCallback<List<Map<String, Object>>>() {public List<Map<String, Object>> doInHibernate(Session session) throws HibernateException,SQLException {List find = null;String sql = "select t1.* ,t2.portalItemId as itemId ,t2._column ,t2._row ,t2._order ,t2.orgRoleId ,t2.projectId, t2.workZoneSiteId ,t2.portalItemHeight,t2.portalItemWidth from portal_item as t1,portal_default_workzonesite_item as t2 where t1.status=1 and t1.id=t2.portalItemId and t2.workZoneSiteId='"+ workZoneSiteId + "'";SQLQuery query = session.createSQLQuery(sql);query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);find = query.list();return find;}});在得到的list拿出相应原本以为Long类型数据进行Long型转换时抛出Exception:ClassCastException: cannot cast java.math.BigInteger to java.lang.Long请问有人遇到过这情况吗,请问发生这种不预期的异常原因在哪?谢谢 问题补充: @Testpublic void getUserPersonalWorkZonesUnderProject() {List<Map<String, Object>> list = workZoneService.getUserPersonalWorkZonesUnderProject(projectId, userId);if(list!=null){for (Map<String, Object> map : list) {Set<String> keys = map.keySet();for (String key : keys) {log.info("key="+key+", value.class="+map.get(key).getClass());}}}logInfo(list);}这是我针对上面hibernate查询写的test,结果如下:[INFO ] [2012-12-17 14:05:16,710] [com.iscreate.plat.portal.test.WorkZoneServiceTest] - key=title, value.class=class java.lang.String[INFO ] [2012-12-17 14:05:16,710] [com.iscreate.plat.portal.test.WorkZoneServiceTest] - key=visible, value.class=class java.lang.Integer[INFO ] [2012-12-17 14:05:16,710] [com.iscreate.plat.portal.test.WorkZoneServiceTest] - key=status, value.class=class java.lang.Integer[INFO ] [2012-12-17 14:05:16,710] [com.iscreate.plat.portal.test.WorkZoneServiceTest] - key=userId, value.class=class java.lang.String[INFO ] [2012-12-17 14:05:16,710] [com.iscreate.plat.portal.test.WorkZoneServiceTest] - key=_order, value.class=class java.lang.Integer[INFO ] [2012-12-17 14:05:16,710] [com.iscreate.plat.portal.test.WorkZoneServiceTest] - key=projectId, value.class=class java.math.BigInteger[INFO ] [2012-12-17 14:05:16,710] [com.iscreate.plat.portal.test.WorkZoneServiceTest] - key=workZoneId, value.class=class java.math.BigInteger[INFO ] [2012-12-17 14:05:16,710] [com.iscreate.plat.portal.test.WorkZoneServiceTest] - key=zoneName, value.class=class java.lang.String
解决方案
你的配置和使用都没问题。1、用的是hibernate哪个版本?2、把异常和代码贴全 (workZoneSiteId 是什么类型?)
解决方案二:
BigInteger的分析其实涉及很多内容,网上都不怎么全面,找了半天,感觉这个系列的教程讲解的还是最详细的:http://swiftlet.net/archives/tag/java-biginteger
解决方案三:
我好像错了,如果你用的是mysql的话,java.lang.Long改为java.math.BigDecimal试试请看以下网址的Table 20.24http://dev.mysql.com/doc/refman/5.0/en/connector-j-reference-type-conversions.html
解决方案四:
<id name="id" type="java.lang.Long">应该是java.lang.long吧