-->
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.  [ 6 posts ] 
Author Message
 Post subject: NumberFormatException on char data
PostPosted: Thu Sep 21, 2006 3:32 pm 
Newbie

Joined: Wed Jun 28, 2006 8:58 am
Posts: 9
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version: 3.1.2:

Code: List results = session.createSQLQuery(sqlQuery).list();

Caused by: com.jnetdirect.jsql.JSQLException: java.lang.NumberFormatException: For input string: "WV"
at com.jnetdirect.jsql.JSQLException.makeFromDriverError(JSQLException.java:70)
at com.jnetdirect.jsql.JSQLStatement.getRowsetField(JSQLStatement.java:867)
at com.jnetdirect.jsql.JSQLResultSet.getInt(JSQLResultSet.java:328)
at com.jnetdirect.jsql.JSQLResultSet.getInt(JSQLResultSet.java:337)
at org.apache.commons.dbcp.DelegatingResultSet.getInt(DelegatingResultSet.java:236)
at org.hibernate.type.IntegerType.get(IntegerType.java:28)
at org.hibernate.type.NullableType.nullSafeGet(NullableType.java:113)
at org.hibernate.type.NullableType.nullSafeGet(NullableType.java:140)
at org.hibernate.loader.custom.CustomLoader.getResultColumnOrRow(CustomLoader.java:183)
at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:595)
at org.hibernate.loader.Loader.doQuery(Loader.java:689)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:224)
at org.hibernate.loader.Loader.doList(Loader.java:2145)


Name and version of the database you are using: MS SQL Server v9.00.2047.00

The generated SQL (show_sql=true):
Hibernate: select rtrim(Accounts.stateCode), rtrim(Accounts.county), rtrim(Accounts.schoolType), rtrim(Accounts.accountN
ame), Accounts.accountID, Accounts.quantity, rtrim(Roles.roleName), ( select count(*) from SessionRecord, Users where Se
ssionRecord.accountID = Accounts.accountID and SessionRecord.beginTime > '08/01/2006' and Users.userID = SessionRecord.u
serID and Users.roleID = Roles.roleID ), ( select count(*) from SessionRecord, Users where SessionRecord.accountID = Acc
ounts.accountID and SessionRecord.beginTime > '08/01/2006' and SessionRecord.beginTime < '09/01/2006' and Users.userID =
SessionRecord.userID and Users.roleID = Roles.roleID ), ( select count(*) from SessionRecord, Users where SessionRecord
.accountID = Accounts.accountID and SessionRecord.beginTime > '09/01/2006' and SessionRecord.beginTime < '10/01/2006' an
d Users.userID = SessionRecord.userID and Users.roleID = Roles.roleID ) from Accounts, Roles where Accounts.internalFlag
= 0 and Accounts.schoolType <> 'SIS' and Accounts.stateCode = 'wv' and ( Roles.roleName = 'Teacher' or Roles.roleName =
'Student' ) order by Accounts.stateCode, Accounts.county, Accounts.accountID, Roles.roleName


Debug level Hibernate log excerpt:
15:12:50,790 DEBUG AbstractBatcher:311 - about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
15:12:50,805 DEBUG ConnectionManager:358 - opening JDBC connection
15:12:50,821 DEBUG DBCPConnectionProvider:316 - active: 1 (max: 8) idle: 0(max: 2)15:12:50,837 DEBUG AbstractBatcher:424 - preparing statement
15:12:51,180 DEBUG AbstractBatcher:327 - about to open ResultSet (open ResultSets: 0, globally: 0)
15:12:51,259 DEBUG AbstractBatcher:334 - about to close ResultSet (open ResultSets: 1, globally: 1)
15:12:51,259 DEBUG AbstractBatcher:319 - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
15:12:51,274 DEBUG AbstractBatcher:470 - closing statement
15:12:51,321 WARN JDBCExceptionReporter:71 - SQL Error: 0, SQLState: null
15:12:51,321 ERROR JDBCExceptionReporter:72 - java.lang.NumberFormatException: For input string: "WV"


The error message is for the rtrim(Accounts.stateCode) value. The stateCode column has type CHAR(2). When I run the same code using the raw column instead of wrapped in RTRIM(), I get a single java Character object instead of the 2-character String. With RTRIM, I get NumberFormatException. My guess is confusion between JDBC driver and Hibernate. I am using latest JSQLConnect. Why is Hibernate even trying to process the output of RTRIM as an integer value?

Any help appreciated.

Thanks,

Greg


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 22, 2006 4:17 am 
Newbie

Joined: Tue May 10, 2005 2:32 pm
Posts: 14
Location: Quito, Ecuador South America
I think JDBC driver is translanting CHAR(2) to Java type char instead of String, try modify column type to varchar(2) instead of CHAR(2)

_________________
Hernan Leon
Mindsoft


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 22, 2006 7:46 am 
Newbie

Joined: Wed Jun 28, 2006 8:58 am
Posts: 9
Most definitely that is the problem in the first case where I select the raw column value. I have VARCHAR() columns for which I do not get this problem. Either JSQLConnect or Hibernate is applying the wrong Java class to the CHAR(2) SQL datatype, converting to Character instead of String. This is an error. I cannot change the schema. I need to determine which component is at fault and try to get the error corrected.

The second problem is different, where I try to select RTRIM() of the CHAR(2) column. This is where I get the NumberFormatException and, in fact, I do want to select using RTRIM(). Why would either JSQLConnect or Hibernate think the result of RTRIM() should be converted to Integer?

Greg


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 22, 2006 9:02 am 
Newbie

Joined: Wed Jun 28, 2006 8:58 am
Posts: 9
I figured this out by tracing in the Hibernate source code. I did not provide aliases for the SELECT fields that involve functions, so they all appear in the Hibernate result set object with an empty string value for "name". When building the Object[] rows to return in List for the query, Hibernate extracts the values from the result set object by this "name" value.

Not sure if there is a way to tell Hibernate to extract by position rather than by name. The alias/name is not needed by me for any other purpose and SQL certainly supports these function SELECT fields with no alias applied. Anyway, for each field in the result row with no "name" value that is requested from the result set object, the value for the first no "name" field value is returned. In my example, the stateCode value is returned for county, schoolType, accountName, roleName, and for the 3 subselect count values. This is what causes the problem. The string stateCode gets interpreted as the value for the numeric count fields.

Work-around is to assign aliases for all SELECT fields. I tested and this works. Not sure if the Hibernate team would consider this to be a bug or working as designed.

Greg


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 22, 2006 6:44 pm 
Newbie

Joined: Tue May 10, 2005 2:32 pm
Posts: 14
Location: Quito, Ecuador South America
I do not know if this is what you are looking for, when you call Native SQL query result returns an Object[] and you can access results jus like this:
Quote:
Iterator i = session.createQuery(
"select item.id, item.description, bid.amount " +
"from Item item join item.bids bid " +
"where bid.amount > 100"
)
.list()
.iterator();
while ( i.hasNext() ) {
Object[] row = (Object[]) i.next();
Long id = (Long) row[0];
String description = (String) row[1];
BigDecimal amount = (BigDecimal) row[2];
// ... show values in a report screen
}

from Hiberntate in Action Chapter 7


You can also try using dynamic instantiation like the following (also from Chapter 7 Hibernate in Action:
Quote:
7.5.4 Native SQL queries
We can think of two good examples why you might use native SQL queries in Hibernate:
HQL provides no mechanism for specifying SQL query hints, and it also
doesn’t support hierarchical queries (such as the Oracle CONNECT BY clause). We
suppose that you’ll stumble on other examples.
In these (relatively rare) cases, you’re free to resort to using the JDBC API
directly. However, doing so means writing the tedious code by hand to transform a
JDBC ResultSet to an object graph. You can avoid all this work by using Hibernate’s
built-in support for native SQL queries.
You only need to learn one trick. An SQL query result may return the state of
multiple entity instances in each row and even the state of multiple instances of the
same entity. You need a way to distinguish between the different entities. Hibernate
uses a naming scheme for the result column aliases to correctly map column values
284 CHAPTER 7
Retrieving objects efficiently
to the properties of particular instances. You wouldn’t want the details of this naming
scheme to be exposed to the user; instead, native SQL queries are specified with
placeholders for the column aliases.
The following native SQL query shows what these placeholders—the names
enclosed in braces—look like:
String sql = "select u.USER_ID as {uzer.id},"
+ " u.FIRSTNAME as {uzer.firstname},"
+ " u.LASTNAME as {uzer.lastname} from USERS u";
Each placeholder specifies an HQL-style property name. When we call this query in
code, we must provide the entity class that is referred to by uzer in the placeholders.


Hope it works

_________________
Hernan Leon
Mindsoft


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 25, 2006 8:12 am 
Newbie

Joined: Wed Jun 28, 2006 8:58 am
Posts: 9
Sorry for the confusion, I know how to utilize the List of Object[] returned by the list() method. My problem was that Hibernate was throwing an exception when converting the underlying result set returned by the JDBC driver code into the List of Object[]. The work-around that I described prevents the exception in the underlying Hibernate code.

Greg


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 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.