-->
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: Parameter binding problem
PostPosted: Mon Jun 27, 2005 1:05 pm 
Newbie

Joined: Mon Jun 27, 2005 11:33 am
Posts: 8
I have run into a situation where using parameter binding fails to return any records for my query, but building the parameter in the query string works. I will provide both code snippets below. There are no exceptions being thrown, just an empty ResultSet.

Hibernate version: 3.0.1

Code between sessionFactory.openSession() and session.close():

Code:
Query q = session.createQuery("select sub.id from Subscription sub "
  + "inner join sub.subscriber.memberships ms "
  + "where ms.matchSet.hash = :h");
q.setString("h", hash);
List results = q.list();

...returns 0 results.

Code:
Query q = session.createQuery("select sub.id from Subscription sub "
  + "inner join sub.subscriber.memberships ms "
  + "where ms.matchSet.hash = '" + hash + "'");
List results = q.list();

...returns 1 record.

It seems to me these two examples should have the same results. What am I doing wrong?

Name and version of the database you are using: Oracle 10g

Debug level Hibernate log excerpt:

First, the parameterized version:
Code:
org.hibernate.SQL DEBUG 27 Jun 2005 11:18:40- select subscripti0_.elig_Sub_Id as col_0_0_ from elig_Sub subscripti0_ inner join elig_Mbr member1_ on subscripti0_.Sub_elig_Mbr_Id=member1_.elig_Mbr_Id inner join elig_MS membership2_ on member1_.elig_Mbr_Id=membership2_.elig_Mbr_Id, pers_MatchSet matchset3_ where (matchset3_.GeneratingHash=?  and membership2_.pers_MatchSet_Id=matchset3_.pers_MatchSet_Id)
org.hibernate.jdbc.AbstractBatcher DEBUG 27 Jun 2005 11:18:40- preparing statement
org.hibernate.loader.hql.QueryLoader DEBUG 27 Jun 2005 11:18:40- bindNamedParameters() 11111222223333344444555556666677 -> h [1]
org.hibernate.type.StringType DEBUG 27 Jun 2005 11:18:40- binding '11111222223333344444555556666677' to parameter: 1
org.hibernate.jdbc.AbstractBatcher DEBUG 27 Jun 2005 11:18:40- about to open ResultSet (open ResultSets: 0, globally: 0)
org.hibernate.loader.Loader DEBUG 27 Jun 2005 11:18:40- processing result set
org.hibernate.loader.Loader DEBUG 27 Jun 2005 11:18:40- done processing result set (0 rows)


The non-parameterized version:
Code:
org.hibernate.SQL DEBUG 27 Jun 2005 12:58:30- select subscripti0_.elig_Sub_Id as col_0_0_ from elig_Sub subscripti0_ inner join elig_Mbr member1_ on subscripti0_.Sub_elig_Mbr_Id=member1_.elig_Mbr_Id inner join elig_MS membership2_ on member1_.elig_Mbr_Id=membership2_.elig_Mbr_Id, pers_MatchSet matchset3_ where (matchset3_.GeneratingHash='11111222223333344444555556666677'  and membership2_.pers_MatchSet_Id=matchset3_.pers_MatchSet_Id)

....

org.hibernate.loader.Loader DEBUG 27 Jun 2005 12:58:30- processing result set
org.hibernate.loader.Loader DEBUG 27 Jun 2005 12:58:30- result set row: 0
org.hibernate.loader.Loader DEBUG 27 Jun 2005 12:58:30- result row:
org.hibernate.type.LongType DEBUG 27 Jun 2005 12:58:30- returning '33' as column: col_0_0_
org.hibernate.loader.Loader DEBUG 27 Jun 2005 12:58:30- done processing result set (1 rows)
[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 28, 2005 9:38 am 
Newbie

Joined: Mon May 30, 2005 6:58 am
Posts: 3
This might happen with Oracle CHAR fields because of their fixed length.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 28, 2005 9:46 am 
Regular
Regular

Joined: Thu Apr 21, 2005 9:05 am
Posts: 50
Location: Boston, U.S
I think it may work if you specify
q.setString("h", "hash"); instead of q.setString("h", hash);


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 06, 2005 5:17 am 
Newbie

Joined: Wed Jul 06, 2005 5:09 am
Posts: 1
I've got the same problem with Hibernate 2.18 and Oracle 9i, How do you solve it?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 13, 2005 2:36 pm 
Newbie

Joined: Mon Jun 27, 2005 11:33 am
Posts: 8
Problem solved. I had to pad the right side of the parameter with spaces so that it was 40 characters long (the length of the column). Works like a charm!


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.