Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

can't cast the first element of an Object[] to String -- results of Hibernate Native SQL query

 
Stuart Rogers
Ranch Hand
Posts: 141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I have the following snippet


that doesn't want to cast the first element to a string.

The first println writes out
[B, 4, Steve Martin, M]
instead of the expected
[BX0223, 4, Steve Martin, M]

The 2nd println writes out
raw 1st element: B
instead of the expected
raw 1st element: BX0233

and the 3rd println throws a CastClassException : java.lang.Character error

Any ideas what's (not) going on here? Why can't I cast the first element while the rest seem fine?


TIA,

Still-learning Steve
 
Paul Clapham
Sheriff
Posts: 21572
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because the first element is a Character (the error message tells you that) and you can't cast a Character to a String.

So apparently Hibernate thinks you mapped the "code" column of your Employees table to a single character. You on the other hand seem to be expecting a multi-character string like "BX0223". You're going to have to look at your Hibernate configuration to find out why that happened.
 
Stuart Rogers
Ranch Hand
Posts: 141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply!

googling around some more turned up the addScalar method you can hang on SQLQuery object. So adjusting my query slightly and using some of these to explicity set the types did the trick.


Explicitly setting alaises for the columns isn't necessary for this simple case, but is demonstrated in case
you wanted to do something more complicated like


CASE CLOSED

Thanks again,

Still-learning Steve
 
Stuart Rogers
Ranch Hand
Posts: 141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and this more-enhanced version lets you place the query results directly into a stone-simple
Java class that is =NOT= managed by Hibernate. Great for generating reports where
you'd like to have a class that corresponds to your query row but don't need to store it in a
database table.

The fields in the POJO exactly match up with the columns in the query.

Assume you have a POJO named RptW


and in the executable



I think this is a powerful technique, hope others enjoy it!

Thanks to all who replied,


Still-learning Steve
 
Paul Clapham
Sheriff
Posts: 21572
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow. It's been a long time since I really looked at Hibernate, but obviously those guys haven't been standing still. It's like I know nothing about it now, I'm going to have to spend some time catching up.

Thanks for the research, Stuart!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic