-->
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.  [ 2 posts ] 
Author Message
 Post subject: Problem org.hibernate.type.NullableType nullSafe in SQLQuery
PostPosted: Thu Feb 04, 2010 8:00 am 
Newbie

Joined: Thu Feb 04, 2010 7:55 am
Posts: 2
Hello I've this simple query that I've to do in native SQL:

Code:
SQLQuery query = sess.createSQLQuery("SELECT * from " + table + " WHERE " + filter + " LIKE '" + search + "%'");
System.out.println(query.toString());
List<Object[]> result = query.list();


so there is no mapping. It works most of the times. The problem is when I use a certain table in a mySQL db with this schema:

Code:
CREATE TABLE `nazioni` (
  `nazid` int(11) NOT NULL AUTO_INCREMENT,
  `nazsigla` varchar(8) NOT NULL DEFAULT '',
  `naznome` varchar(50) NOT NULL DEFAULT '',
  `nazcontinente` varchar(50) DEFAULT NULL,
  `naziso` varchar(4) DEFAULT NULL,
  `nazeuro` char(1) DEFAULT NULL,
  `nazvalucod` varchar(10) DEFAULT NULL,
  `nazvaluid` int(11) DEFAULT NULL,
  `nazcod` varchar(4) DEFAULT NULL,
  `nazcbe` varchar(4) DEFAULT NULL,
  `nazcitt` varchar(50) DEFAULT NULL,
  `nazstato` char(1) DEFAULT NULL,
  `naznomeiso` varchar(50) DEFAULT '',
  `nazisoex` varchar(10) DEFAULT '',
  `nazlinguaid` int(11) DEFAULT '-1',
  `nazlingua` varchar(50) DEFAULT '',
  PRIMARY KEY (`nazid`),
  UNIQUE KEY `naznome` (`naznome`),
  KEY `nazsigla` (`nazsigla`)
) ENGINE=MyISAM AUTO_INCREMENT=244 DEFAULT CHARSET=latin1


and I shouldn't modify this. The problem is with the field
Code:
nazeuro
that sometimes is NULL. It causes this exception:

Code:
4-feb-2010 12.53.42 org.hibernate.type.NullableType nullSafeGet
INFO: could not read column value from result set: nazeuro; String index out of range: 0
java.lang.StringIndexOutOfBoundsException: String index out of range: 0
        at java.lang.String.charAt(String.java:686)
        at org.hibernate.type.CharacterType.get(CharacterType.java:29)
        at org.hibernate.type.NullableType.nullSafeGet(NullableType.java:163)
        at org.hibernate.type.NullableType.nullSafeGet(NullableType.java:189)
        at org.hibernate.loader.custom.CustomLoader$ScalarResultColumnProcessor.extract(CustomLoader.java:474)
        at org.hibernate.loader.custom.CustomLoader$ResultRowProcessor.buildResultRow(CustomLoader.java:420)
        at org.hibernate.loader.custom.CustomLoader.getResultColumnOrRow(CustomLoader.java:317)
        at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:606)
        at org.hibernate.loader.Loader.doQuery(Loader.java:701)
        at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:236)
        at org.hibernate.loader.Loader.doList(Loader.java:2220)
        at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2104)
        at org.hibernate.loader.Loader.list(Loader.java:2099)
        at org.hibernate.loader.custom.CustomLoader.list(CustomLoader.java:289)
        at org.hibernate.impl.SessionImpl.listCustomQuery(SessionImpl.java:1695)
        at org.hibernate.impl.AbstractSessionImpl.list(AbstractSessionImpl.java:142)
        at org.hibernate.impl.SQLQueryImpl.list(SQLQueryImpl.java:152)
        at Views.Components.QuickLookup$updater.run(QuickLookup.java:91)


How can I solve this, possibly without touching the DB


Top
 Profile  
 
 Post subject: Re: Problem org.hibernate.type.NullableType nullSafe in SQLQuery
PostPosted: Thu Feb 04, 2010 5:39 pm 
Regular
Regular

Joined: Mon Jan 05, 2009 6:42 pm
Posts: 99
Location: IL
So the problem happens when the "filter" is "nazeuro" ??
Ok modify the query like this and it Might work depending on your database:-
Code:
SQLQuery query = sess.createSQLQuery("SELECT * from " + table + " WHERE " + filter + " LIKE '" + search + "%' +" AND "+filter+ is not null");
System.out.println(query.toString());
List<Object[]> result = query.list();


hope this helps!
Srilatha.


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