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
posted
0
See, in this case you will get an array of Object.
Navigate through the array for the elements. Hope you understand it.
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
posted
0
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.