-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: Problem with SQL query returning CHAR(20) as Character
PostPosted: Fri Mar 02, 2007 6:47 am 
Newbie

Joined: Wed Jan 17, 2007 11:50 am
Posts: 10
Hello,

I'm having trouble with a simple SQL query run through Hibernate. It looks like this:

List results = session.createSQLQuery("SELECT name FROM table").list();

"name" is a CHAR(20) column in a DB2 database. What Hibernate returns is a list of java.lang.Characters - the names are truncated and only the first character is returned.

Right now I'm working around this by calling addScalar("name", Hibernate.STRING).

Apparently, Hibernate always transforms CHAR(n) columns into Characters even for n>1. Is this a bug or intended behaviour?

Thanks for any insights.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 02, 2007 11:43 am 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
what is the java data type declared in your mapping file?

_________________
Chris

If you were at work doing this voluntarily, imagine what you'd want to see to answer a question.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 02, 2007 11:48 am 
Newbie

Joined: Wed Jan 17, 2007 11:50 am
Posts: 10
The type in the mapping file is "string". But I don't think Hibernate uses the mapping in this case, since I don't call "addEntity" on the query instance to specifiy an entity result type. I want to get scalar values from the query.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 29, 2007 10:53 am 
Newbie

Joined: Wed Aug 29, 2007 10:46 am
Posts: 1
I've just run into the same issue. I have a CHAR(11) field, mapped as a string. When using hibernate's SQLQuery, the result set is a list of java.lang.Character objects rather than the expected java.lang.String.

To get around this I'm currently using the SQLConnection exposed via the hibernate Session's connection() method. I'm not too enthusiastic about using a deprecated method such as connection(), but it seems to be the best bet for now.


BTW, I'm only using SQL because I needed a simple work around for this bug:
http://opensource.atlassian.com/project ... e/HHH-2038


Top
 Profile  
 
 Post subject: Re: Problem with SQL query returning CHAR(20) as Character
PostPosted: Fri Nov 19, 2010 9:27 am 
Newbie

Joined: Sat Oct 18, 2008 11:34 am
Posts: 10
Although this thread is ancient, here's a solution for people with the same problem:

When using SQL queries, addScalar() works fine. But when you use a nativeQuery, such as in:
Code:
Query q = entityManager.createNativeQuery("select name from table");

there is no addScalar method, so I usually add a trim() function around the CHAR field, which also trims the spaces:
Code:
Query q = entityManager.createNativeQuery("select trim(name) from table");


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.