• 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

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

 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 160
IntelliJ IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Maya sekar
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you so much. I understand now...

 
Riaan Nel
Ranch Hand
Posts: 160
IntelliJ IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are most welcome!
reply
    Bookmark Topic Watch Topic
  • New Topic