我在执行java.lang.ClassCastException: java.math.BigInteger cannot be cast to java.lang.Long
时遇到int totalCount=criteria.list().size();
异常。
请帮我确定原因和解决方案。
public GridPageDet list(DwrParam dwrParam,UserFound user,JobFound job) throws Exception {
Query query = getSession().createSQLQuery(
"select user_id from hs_cust_users where cust_id IN(select cust_id from customers where user_id=:userId)").setParameter("userId", user.getId());
Collection<Object[]> list = (Collection<Object[]>)query.list();
Criteria criteria=getSession().createCriteria(Filter.class);
criteria.createCriteria("filter.typeId", "filterType", Criteria.FULL_JOIN);
criteria.add(Expression.eq("status", 1));
if(user!=null && user.getId()!=null){
Object statusArr [] = {1};
criteria.createCriteria("user", "user", Criteria.FULL_JOIN);
criteria.add(Expression.in("status", statusArr));
if(user.getAccess().getId().intValue() == Helper.priv.intValue() ||
user.getAccess`enter code here`().getId().intValue() == Helper.id.intValue()){
criteria.add(Expression.in("user.id", list));
}else{
criteria.add(Expression.eq("user.id", user.getId()));
}
}
int totalCount=criteria.list().size();
}
答案 0 :(得分:2)
没有看到完整的堆栈跟踪(你可以发布吗?),我怀疑问题出在你的标准的“user.id”部分。我看到你正在针对user.getAccessGroupMap()的返回值调用.intValue()。getId();该值的返回类型是什么,它是否与POJO的id属性映射到的类型相匹配?
您确定列表变量包含与您期望的类型匹配的类型的对象吗?您似乎在if条件下将本机SQL与Hibernate代码混合在一起,并且在它们之间切换时很容易让事情变得不对劲。即使这不是你的问题,你也应该强烈地键入(即转换为比Collection<Object[]>
更精确的东西)你的列表变量,所以你很早就发现它的内容类型不是符合你期待的......
答案 1 :(得分:0)
使用Java
Math.BigInteger.longValue()
从BigInteger
转换为Long
答案 2 :(得分:0)
public GridPageDet listFilter(DwrParam dwrParam,User user,Job job) throws Exception {
Query query = getSession().createSQLQuery(
"select user_id from hs_cust_users where cust_id IN(select cust_id from hs_cust_users where user_id=:userId)").setParameter("userId", user.getId());
Collection<Object> list = (Collection<Object>)query.list();
List<Long> l=new ArrayList<Long>();
for(Object obj : list){
l.add(Long.parseLong(obj+""));
}
GridPageDet gridPgeDet=new GridPageDet();
Criteria criteria=getSession().createCriteria(Filter.class);
criteria.createCriteria("filterQA.typeId", "filterQAType", Criteria.FULL_JOIN);
criteria.add(Expression.eq("status", 1));
if(user!=null && user.getId()!=null){
Object statusArr [] = {1};
criteria.createCriteria("user", "user", Criteria.FULL_JOIN);
criteria.add(Expression.in("status", statusArr));
if(user.getAccessGroupMap().getId().intValue() == Helper.ACCESS_GROUP_MAP_COMPANY_ADMIN_ID.intValue() ||
user.getAccessGroupMap().getId().intValue() == Helper.ACCESS_GROUP_MAP_COMPANY_DIRECTOR_USER_ID.intValue()){
criteria.add(Expression.in("user.id", l));
}else{
criteria.add(Expression.eq("user.id", user.getId()));
}
}
List<ExtJSGridFilter> extJsFilterList = (List<ExtJSGridFilter>)dwrParam.getFilter();
int totalCount=criteria.list().size();
}
答案 3 :(得分:-1)
尝试使用此int count = ((Long) criteria.list().size()).intValue();