关于hibernate:java.lang.ClassCastException:java 1.6中的java.lang.Long无法强制转换为java.lang.Integer |
您所在的位置:网站首页 › object转为long › 关于hibernate:java.lang.ClassCastException:java 1.6中的java.lang.Long无法强制转换为java.lang.Integer |
即使我将Object强制转换为int,但也会发生此异常... 实际上我的Hibernate-JPA方法返回了Object,然后将那个Object转换为int ... 这是我的hibernate代码: 12345678@Transactional public Object getAttendanceList(User user){ Query query = entityManager.createQuery("select Count(ad) from AttendanceDemo ad inner join ad.attendee at where at.user=:user", Long.class); query.setParameter("user", user); return query.getSingleResult(); }现在我正在将此Object转换为int: 1int k = (Integer) userService.getAttendanceList(currentUser);我正在将Object转换为Integer。 相关讨论 看起来getAttendanceList正在返回Long。您可以张贴相同的代码吗 您确定要返回整数吗? ClassCastException表示您不返回整数... 是的。我想将getAttendanceList()方法的返回值存储到k中。然后只有ClassCastException出现,现在我要转换为(Integer),但是再次出现该异常。使用: 1((Long) userService.getAttendanceList(currentUser)).intValue();代替。 .intValue()方法是在Long扩展的类Number中定义的。 相关讨论 但是IDE显示...未定义类型Object的方法intValue() 为什么不使用((Number) userService.getAttendanceList(currentUser)).intValue(); @ johnchen902是的,没错。但是在这种情况下,我不确定OP是否真的想要那样。否则数据库模型将在一夜之间从Long更改为其他数字类型;) 哦...谢谢.. fge ..它正在工作...但是当我们这样写时:((Number)userService.getAttendanceList(currentUser))。intValue();它为什么不起作用??? @ user2496295嗯!那应该工作...或者您还有另一个Number类,它不是JDK的? @ user2496295那不是我的意思。看你的进口。查看什么是Number的导入。它应该是java.lang.Number。结果的数量可以(理论上)大于整数的范围。我将重构代码并改为使用返回的long值。 |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |