IntelliJ Java IDE
The moose likes Object Relational Mapping and the fly likes problem with ClassCastException Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Object Relational Mapping
Reply Bookmark "problem with ClassCastException" Watch "problem with ClassCastException" New topic
Author

problem with ClassCastException

navdeep thakur
Greenhorn

Joined: May 09, 2007
Posts: 18

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


Navdeep Thakur alias Navi
krishna bulusu
Ranch Hand

Joined: Aug 28, 2006
Posts: 184
See, in this case you will get an array of Object.

Navigate through the array for the elements.
Hope you understand it.


Thanks&Regards, Krishna.
SCJP1.4, SCWCD1.4, SCBCD 5.0
navdeep thakur
Greenhorn

Joined: May 09, 2007
Posts: 18

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

Joined: May 09, 2007
Posts: 18

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

Joined: Oct 05, 2001
Posts: 1158

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

Joined: May 09, 2007
Posts: 18

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
Marshal

Joined: Mar 22, 2005
Posts: 32767
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.


Android appsImageJ pluginsJava web charts
 
IntelliJ Java IDE
 
subject: problem with ClassCastException
 
Threads others viewed
Hibernate
jsp:useBean id not clear
Hibernate Question
Hibernate Question
Problem in hibernate join result
WebSphere development made easy
without the weight of IBM tools
http://www.myeclipseide.com