-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: ClassCastException in hibernate
PostPosted: Wed Jun 22, 2011 9:11 am 
Newbie

Joined: Mon Jun 20, 2011 7:39 am
Posts: 6
Hi

I want to execute select query on my table using hibernate,
it works correctly and I get Iterator from query,when I cast Object of Iterator to my class(named Person),
it show this exception:
java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to com.domain.Person
someone help me please.
here is my code of my query:

Code:
public LinkedList select(){
      
      LinkedList list= new LinkedList();
      org.hibernate.Transaction tx;
      
      sessionFactory= (SessionFactory) HibernateUtil.getSessionFactory();
      session =sessionFactory.openSession();
      
      tx= (org.hibernate.Transaction) session.beginTransaction();
      
      try{
         
          Query query= session.createQuery("select person.code1,person.name,person.debtorCurrency from " +  Person.class.getName()+ " person");
         
          Collection col= (Collection) query.list();
          list.addAll(col);
         
         tx.commit();
         
      }catch(Exception e){
         
         tx.rollback();
         e.printStackTrace();
         
      }finally{
         session.close();
         return list;
      }
      
   }


Top
 Profile  
 
 Post subject: Re: ClassCastException in hibernate
PostPosted: Wed Jun 22, 2011 9:56 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
When you specify a list of properties in the select statement Hibernate will return the result as a List containing Object[] elements. To get a List of Person object you should write your query like this: select person from Person person


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.