*
The moose likes Object Relational Mapping and the fly likes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark ""java.lang.String cannot be cast" Error in Hibernate  " Watch ""java.lang.String cannot be cast" Error in Hibernate  " New topic
Author

"java.lang.String cannot be cast" Error in Hibernate

Maya sekar
Ranch Hand

Joined: Sep 22, 2011
Posts: 70
Hi Everyone,


I was working on a simple Hibernate sample program,


something I copied off RoseIndia. From what i can see the code is error free, but when I run it I get the following error msg,


Hibernate: select bean0_.username as col_0_0_ from bean bean0_ where bean0_.id='3'
java.lang.String cannot be cast to my.apps.Bean

I initially assumed my HQL Query was wrong, but when I paste the query directly in the database and run it, I get the resultset... but through the progam I get the error msg pasted above. Can anyone help me figure out what I have done wrong?

Thank you
Riaan Nel
Ranch Hand

Joined: Apr 23, 2009
Posts: 157

Hi Maya

Look at the following two lines of your code;

You are selecting b.userName (which looks like a String), but you are trying to cast that String to your Bean class. If you execute an HQL query with a single field, the returned list will contain objects of the type of that field. I.e., you are getting a list of String objects.

If you want to select Bean objects, your query should look like this;


Cheers,
Riaan


"The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man." - George Bernard Shaw
Maya sekar
Ranch Hand

Joined: Sep 22, 2011
Posts: 70
Thank you so much. I understand now...

Riaan Nel
Ranch Hand

Joined: Apr 23, 2009
Posts: 157

You are most welcome!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: "java.lang.String cannot be cast" Error in Hibernate
 
Similar Threads
I want to use List<String> in JPQL syntaxis
Delete Query problem: Failure to Commit HQL Delete Transaction
Unable to fetch results using createQuery
HQL Inner Join problem
Servlet not dircting to the next page