File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Object Relational Mapping and the fly likes Problem in Hibernate Query Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Problem in Hibernate Query" Watch "Problem in Hibernate Query" New topic

Problem in Hibernate Query

Anupam Dee
Ranch Hand

Joined: Oct 18, 2010
Posts: 42

This is a simple code to retrieve the expenditure from the expenditure table.
But I am getting ClassCastException Object can't be cast to Expenditure.
How to do it?
And when i used this code to print the result object, i am getting two objects..which is correct..
Now please help me how to do it? I just want to use it as a Expenditure object.
I have XML file expenditure.hbm.xml mapped to the table expenditure and the class is com.expensemanager.core.Expenditure so that i can call the methods over it.
Ranganathan Kaliyur Mannar

Joined: Oct 16, 2003
Posts: 1101

I think this will be answered better in the ORM forum (where Hibernate questions are answered). Moving this to the ORM forum.

harshvardhan ojha
Ranch Hand

Joined: Jul 26, 2007
Posts: 157

Hi Anupam, you are using here sql query. Thats why you are not getting it mapped to POJO. Wither map it to your pojo or use equivalent HQL query. Hope this will help you.
Bill Gorder

Joined: Mar 07, 2010
Posts: 1682

harshvardhan is correct. You are using native SQL, when what you want is to use HQL. I have included a link below that should get you started with getting your query converted to HQL.

[How To Ask Questions][Read before you PM me]
Anupam Dee
Ranch Hand

Joined: Oct 18, 2010
Posts: 42

Yeah thanks folks. I used the Query object and changed the sql to "from Expenditure where...." and it worked.
I agree. Here's the link:
subject: Problem in Hibernate Query
It's not a secret anymore!