0

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();
}
3
  • At what place you are getting this exception? Commented Jul 24, 2013 at 4:13
  • try defining long totalCount Commented Jul 24, 2013 at 4:19
  • Separate criteria.list and list.size and see which one actually throws the error. Due to the List interface definition it cannot be due to list.size operation. Commented Jul 24, 2013 at 4:35

4 Answers 4

2

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...

Sign up to request clarification or add additional context in comments.

Comments

0

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

BigInteger.longValue()

Comments

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

Comments

-1

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

3 Comments

As Thihara pointed out in the comment on the original post, criteria.list() returns a List, and List.size() returns an int. Casting it to Long and then calling intValue() has no effect (other than to confuse the reader), so not having those calls isn't not the cause of the OP's exception.
criteria.list.size can return the value which can not be suffix by Interger.So better you take it in long.But if you want intValue.you can get it in this way
As I said in my first comment on your answer: criteria.list() returns a List, and List.size() returns an int. Casting that to Long and then calling intValue() takes your int and turns it into an int, and so is not useful or meaningful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.