Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I tried to convert the List I got from Hibernate to AdminPopulate type using the following code, and I got this error

Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to model.AdminPopulate

public class Test
{
public static void main(String[] args)
{
     SubUser subUser=new SubUser();        

     List subUserList=subUser.getSubUserAll();

     List<AdminPopulate> subUserListAdminPop=new ArrayList<AdminPopulate>();

     for(Object p:subUserList)
     {
         subUserListAdminPop.add((AdminPopulate)p);
     }         

     for(AdminPopulate p:subUserListAdminPop)
     {
         System.out.println("first ="+p.getFirstName());
     }
 }

}

....and this is the getSubUserAll() method

 public List getSubUserAll()
{
    Session session=null;
    List resultsList=null;

    try
    {
       CreateHibernateSession hbSession=new CreateHibernateSession();

       session=hbSession.getHBSession();

       String sql = "SELECT SubUser.FirstName,\n" +
                    "SubUser.LastName,\n" +
                    "SubUser.UserName,\n" +
                    "SubUserType.Name\n" +
                    "FROM SubUser\n" +
                    "INNER JOIN SubUserType ON SubUser.idSubUserType = SubUserType.idSubUserType\n" +
                    "WHERE SubUserType.idSubUserType !=  0";

       Query query = session.createSQLQuery(sql);  

       resultsList = query.list(); 

       return resultsList;
    }
    catch(Exception ex)
    {
        ex.printStackTrace();
    }
    finally
    {
        session.close();
    }
    return resultsList;
}
share|improve this question
    
It means you're casting an instance of Object to model.AdminPopulate which isn't an acceptable cast. –  Sufian Feb 10 at 10:14
    
@Sufian: I am sure he already knows that... –  JustCause Feb 10 at 10:17
    
@JustCause wishful thinking. He wouldn't be asking like that if that was true. –  Sufian Feb 10 at 10:22
    
@Sufian I am asking how to solve this. I already know that it isn't an acceptable cast. Looking for a way around it –  TheShadow123 Feb 10 at 10:26

2 Answers 2

If SubUser class is mapped properly to the result of the query, then you can just call query.addEntity(SubUser.class) prior to executing the query.

Query query = session.createSQLQuery(sql);  
query.addEntity(SubUser.class);
resultsList = query.list(); 
share|improve this answer

I am not an expert in Hibernate, but you are trying to cast an array of objects
(Object[] -> [Ljava.lang.Object;), what means, that the method Query.list has returned multiple results in a row. And if you look at your SQL, it is clear why, you are referencing two tables in the select.
I guess Query.list returned in every row an Object[2] with a SubUser in the first and a SubUserType in the second element.

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.