• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ClassCastException Hibernate

 
shriram iyer
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greetings,

My hibernate configuration file is as follows



my hibernate mapping file is as follows



my bookpojo is as follows



my main class is as follows



when i run the program i am getting following error


Hibernate: select this_.name as y0_ from books this_
Exception in thread "main" java.lang.ClassCastException: java.lang.String cannot be cast to hiberbatchtest.BookPojo
at hiberbatchtest.Main.main(Main.java:17)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)


cheers
 
Hemant Thard
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi shriram,


Hibernate: select this_.name as y0_ from books this_


from this query, i think its clear that hibernate is returning only name property which is of type String.
That is why you are getting java.lang.ClassCastException.

if you want to retrieve the whole object remove this from from your criteria query.


here is the link you can refer to.




 
shriram iyer
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks
 
Waswani Naresh
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want to retrieve only the names than remove the below line.

BookPojo obj= (BookPojo) it.next();


You would actually get a List of String in this case which you can traverse over.

Regards,
Naresh
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic