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();
}
skmaran.nr.iras
  • 8,152
  • 28
  • 81
  • 116

4 Answers4

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

Tim
  • 2,027
  • 15
  • 24
0

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

BigInteger.longValue()

Saher Ahwal
  • 9,015
  • 32
  • 84
  • 152
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();
}
skmaran.nr.iras
  • 8,152
  • 28
  • 81
  • 116
-1

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

MayurB
  • 3,609
  • 2
  • 21
  • 36
  • 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. – Tim Jul 24 '13 at 21:55
  • 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 – MayurB Jul 25 '13 at 04:33
  • As I said in my first comment on your answer: [criteria.list()](https://docs.jboss.org/hibernate/orm/3.6/javadocs/org/hibernate/Criteria.html#list%28%29) returns a List, and [List.size()](http://docs.oracle.com/javase/1.5.0/docs/api/java/util/List.html#size%28%29) 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. – Tim Jul 26 '13 at 22:16