Dismiss
Announcing Stack Overflow Documentation

We started with Q&A. Technical documentation is next, and we need your help.

Whether you're a beginner or an experienced developer, you can contribute.

Sign up and start helping → Learn more about Documentation →

if i am not do type casting to query.list(); to (List)query.list(); it gives me error like error.png and error1.pngPlease help me, I am getting this error

java.lang.ClassCastException: java.util.ArrayList cannot be cast to antlr.collections.List

on calling a method

category= new Category();
ArrayList data= (ArrayList) category.getCategoryInfo();

and method is

public List  getCategoryInfo(){
      Session session = HibernateUtil.openSession();

      String sql = "SELECT category.id as id, category.name,c.name as parent_category FROM category join category as c on c.id=category.parent_id"; 
      SQLQuery query = session.createSQLQuery(sql);
      query.setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP); 
      List data = (List) query.list();
      return data;
}

Please help me what is wrong with this code.

share|improve this question
    
show your imports for List interface. Do you know what kind of List is returned by query.list(); ? – Sabir Khan Feb 4 at 5:46
    
Model Class Imports are as follow package com.jaangu.model; import java.io.Serializable; import java.sql.Date; import java.util.ArrayList; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; import org.hibernate.Criteria; import org.hibernate.SQLQuery; import org.hibernate.Session; import com.jaangu.hibernate.util.HibernateUtil; import antlr.collections.List; – Khushbu Soni Feb 4 at 6:02
    
And Service class Imports are: package com.jaangu.service; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Map; import org.hibernate.Criteria; import org.hibernate.SQLQuery; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import com.jaangu.hibernate.util.HibernateUtil; import com.jaangu.model.Category; import com.jaangu.model.User; – Khushbu Soni Feb 4 at 6:04
    
Why are you doing import antlr.collections.List; in your Model? Should it be java.util.List? Again , Do you know what kind of List is returned by query.list();? – Sabir Khan Feb 4 at 6:14
    
Thanks it works for me. :-) – Khushbu Soni Feb 4 at 9:18
up vote 2 down vote accepted

You need to change line, List data = (List) query.list(); to List data = query.list(); as query.list(); always returns a List so no need to explicit casting.

Also , at assignment part, you again need to program on Interface List but you are doing on concrete class - ArrayList.

ArrayList data= (ArrayList) category.getCategoryInfo(); needs to be changed to List data= category.getCategoryInfo();

When you program on super type interfaces, need to explicit casting is eliminated.

Note that List interface returned from method getCategoryInfo() and one written at assignment part should be the same interfaces ( either java.util.List OR antlr.collections.List , you can't mix the two ).

Figure out on your own about the kind of List returned by query.list();

share|improve this answer

It seems like you've imported the wrong List class. Look at the imports of your class and replace antlr.collections.List for java.util.List.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.