• 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:
  • Devaka Cooray
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
Bartenders:

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

 
Ranch Hand
Posts: 141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 28492
113
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 28492
113
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Get me the mayor's office! I need to tell her about this tiny ad:
Paul Wheaton's 16th Kickstarter: Gardening playing cards for gardeners and homesteaders
https://coderanch.com/t/889615/Paul-Wheaton-Kickstarter-Gardening-playing
reply
    Bookmark Topic Watch Topic
  • New Topic