简体   繁体   中英

Java class cast exception with hibernate criteria query

I am getting java.lang.ClassCastException: java.math.BigInteger cannot be cast to java.lang.Long exception while executing int totalCount=criteria.list().size(); . Please help me to identify the reason and a solution.

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

Without seeing the full stack trace (can you post it?), I suspect the problem is in the "user.id" portion of your criteria. I see you're calling .intValue() against the return value from user.getAccessGroupMap().getId(); what is the return type of that value, and does it match up with the type to which the id property of your POJOs are mapped?

And are you sure that your list variable contains objects of a type that matches up with what you're expecting? You appear to be mixing native SQL with Hibernate code in the if condition, and it's easy to have things not go right when switching between them. Even if this isn't your problem, you'd do well to strongly type (ie cast to something more precise than Collection<Object[]> ) your list variable, so you find out early on if the types of its contents don't match what you're expecting...

使用Java Math.BigInteger.longValue()方法从BigInteger转换为Long

BigInteger.longValue()

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

尝试使用此int count = ((Long) criteria.list().size()).intValue();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM