-->
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.  [ 4 posts ] 
Author Message
 Post subject: Trying to use empty string '' in named parameter
PostPosted: Wed Jan 24, 2007 6:54 am 
Newbie

Joined: Wed Jan 24, 2007 6:45 am
Posts: 4
Hi. I am having some issues trying to get my query (which contains named parameters) to work successfully. Consider the query;

SELECT * FROM A WHERE A.COL = :myParam

In my code I am setting the named parameter depending on some user input, in one case it is set such as;

Query sqlQuery = session.getNamedQuery("QueryName");
sqlQuery.setString("myParam", "M");

and that works fine. But when I want to retrieve all rows where the COL value is equal to '' then I can't get it to work. I have tried (individually);

sqlQuery.setString("myParam", null);
sqlQuery.setString("myParam", ""); // 2 double quotes
sqlQuery.setString("myParam", "''"); // double quote, 2 single quotes, double quote

None of those work, the query runs fine but it is returning 0 rows. Is there a way to set the named parameter to be an empty string?

Hibernate version: 3.1.3

Name and version of the database you are using: SQLServer 7


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 24, 2007 7:25 am 
Newbie

Joined: Sun Jan 21, 2007 10:56 pm
Posts: 14
can i suggest an alternative?

if you are retrieving all the entries, you can probably just use criteria:

Criteria criteria = sess.createCriteria(Table_Name.class);
criteria.setMaxResults(50); //maximum of 50 entries
List list = crit.list();

sess is the hibernate session.

HQL is good when the query you are trying to do is too complicated. Best leave hibernate to optimize etc.


hth,

pali


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 24, 2007 7:30 am 
Newbie

Joined: Wed Jan 24, 2007 6:45 am
Posts: 4
I should have explained in my first post. The front end needs to see the data in 3 ways:

1. All rows
2. Rows where COL = 'M'
3. Rows where COL = '' (or not 'M')

In the database, COL only contains the values '' or 'M'.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 24, 2007 11:24 am 
Newbie

Joined: Wed Jan 24, 2007 6:45 am
Posts: 4
Finally figured it out although I don't understand exactly why it works this way. Need to set the parameter to be (" ") i.e. have a space character in between the double quotes.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.