• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

problem with ClassCastException

 
Greenhorn
Posts: 18
Tomcat Server Java Google App Engine
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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....
 
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Tomcat Server Java Google App Engine
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Tomcat Server Java Google App Engine
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1170
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Tomcat Server Java Google App Engine
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Try 100 things. 2 will work out, but you will never know in advance which 2. This tiny ad might be one:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic