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]