-->
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: Still problems with parameter binding...
PostPosted: Fri Jan 23, 2004 7:21 am 
Beginner
Beginner

Joined: Thu Jan 22, 2004 6:16 am
Posts: 40
Location: Luxembourg
I want to bind a value to a parameter. But I just can't get it to work :

This code SUCCEEDS i.e I get 1 record out from the database :

hql = session.createQuery("SELECT f FROM com f WHERE f.id = '4028fb7a'" );
list = session.find(hql.getQueryString());

This code DOESN'T SUCCEED i.e I get 0 records from the database :

String key = "4028fb7a";
hql = session.createQuery("SELECT f FROM com f WHERE f.id = ':key'" );
hql.setParameter("key", key);
list = session.find(hql.getQueryString());

Could some please help me out why it doesn't work ? And more importantly, how to make it work ?

Regards


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 23, 2004 7:25 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Dont put ' around the :key parameter


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 23, 2004 9:04 am 
Beginner
Beginner

Joined: Thu Jan 22, 2004 6:16 am
Posts: 40
Location: Luxembourg
Sorry, but the quotes are not part of the query. I have removed them.
So the code look like :

String key = "4028fb7a";
hql = session.createQuery("SELECT f FROM com f WHERE f.id = :key" );
hql.setParameter("key", key);
list = session.find(hql.getQueryString());

But I get the following exception when running this code :

net.sf.hibernate.util.JDBCExceptionReporter - JZ0SA: Prepared Statement: Input parameter not set, index: 0.

net.sf.hibernate.util.JDBCExceptionReporter - Could not execute query

java.sql.SQLException: JZ0SA: Prepared Statement: Input parameter not set, index: 0.

Ideas ?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 23, 2004 11:39 am 
Beginner
Beginner

Joined: Thu Jan 22, 2004 6:16 am
Posts: 40
Location: Luxembourg
The solution is as follows :

Instead of having :

String key = "4028fb7a";
hql = session.createQuery("SELECT f FROM com f WHERE f.id = :key" );
hql.setParameter("key", key);
list = session.find(hql.getQueryString());

it should be like this ( only last line of code changed )

String key = "4028fb7a";
hql = session.createQuery("SELECT f FROM com f WHERE f.id = :key" );
hql.setParameter("key", key);
list = hql.list();

The next question is just for curiousity, what is the difference between

1.list = session.find(hql.getQueryString()); // Returns nothing
2.list = hql.list(); // Returns what I want ( 1 record )


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 23, 2004 1:18 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Oh my, I did not see this. session.find(hql.getQueryString()); is absolutely useless, do not do this. Just stick to the examples and use the query class like it is intended.


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.