nr.iras.sk
nr.iras.sk

Reputation: 8498

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

Upvotes: 0

Views: 3355

Answers (4)

Tim
Tim

Reputation: 2027

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 (i.e. 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...

Upvotes: 2

nr.iras.sk
nr.iras.sk

Reputation: 8498

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

Upvotes: 0

MayurB
MayurB

Reputation: 3649

Try to use this int count = ((Long) criteria.list().size()).intValue();

Upvotes: -1

Saher Ahwal
Saher Ahwal

Reputation: 9237

Use Java Math.BigInteger.longValue() Method to convert from BigInteger to Long

BigInteger.longValue()

Upvotes: 0

Related Questions