• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

problem with ClassCastException

 
navdeep thakur
Greenhorn
Posts: 18
Google App Engine Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,
I am very new to Hibernate and trying to learn it. I have developed a small application which will fetch results from the database(Postgres) with the specified "where" condition.
But I am getting an Exception:
java.lang.ClassCastException: [Ljava.lang.Object;
at com.myapps.GetData.main(GetData.java:46)


My code is given below:
1) For Bean Class




2) Code for main class:





Please please help me out....
 
krishna bulusu
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See, in this case you will get an array of Object.

Navigate through the array for the elements.
Hope you understand it.
 
navdeep thakur
Greenhorn
Posts: 18
Google App Engine Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Krishna,
I had tried your recommended way, but Iterator doesn't have nextElement() method, So can you please elaborate it.

Thanks
 
navdeep thakur
Greenhorn
Posts: 18
Google App Engine Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have one query, as it is confusing me from today.

If i need to retrieve a record from the database through select statement, I use object of Class Query.. right.
So which import do i use either

import org.hibernate.Query or
import javax.persistence.Query


First clear me this think, as i noticed today.
Thanks
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are dealing with hibernate so use the hibernate class.

The easiest way to figure out the problem is to stop casting, and use Object class. Then open your debugger and see what the type really is.

Exactly where are you getting the exception?
[ July 30, 2007: Message edited by: Mr. C Lamont Gilbert ]
 
navdeep thakur
Greenhorn
Posts: 18
Google App Engine Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Mr. C Lamont Gilbert,
Actually I am getting ClassCastException at the following point:

UserBean user = (UserBean) iterator.next();

Where UserBean is my bean class where i have my getter and setter methods.
The code for UserBean class is given above. Please have a look.

Thanks
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you have a query like "Select user.id, user.firstname, user.password, user.emailaddress from UserBean where ...", then you will get back an Object[], not a UserBean, as was pointed out above.

If you want an array of UserBean objects, change the query to something like "from UserBean where ..." (without the SELECT part).

If you want one particular UserBean, you can use "(UserBean) sess.load(UserBean.class, user.id)", assuming user.id is the primary key of UserBean.

Liastly, the name of the method for getting the next element of an Iterator is not netxElement, as you found out, but can easily be determined by looking at the javadocs.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic