-->
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.  [ 1 post ] 
Author Message
 Post subject: Single quotes and hql
PostPosted: Mon Apr 16, 2007 6:32 am 
Regular
Regular

Joined: Fri May 05, 2006 11:54 am
Posts: 51
Hi all,

I have a weird problem involving hql and single quotes. I have the following code:

string hql = "from Recipe where ";
List<string> queryParts = new List<string>();

Code:
if(!string.IsNullOrEmpty(recipe.Title))
    queryParts.Add(string.Format("Title LIKE '%{0}%'", recipe.Title.Replace("'", "''")));
if(!string.IsNullOrEmpty(recipe.RecipeId))
    queryParts.Add(string.Format("RecipeId LIKE '%{0}%'", recipe.RecipeId));
for (int i = 0; i < queryParts.Count; i++)
{
    hql += queryParts[i];
    if (i == (queryParts.Count - 1))
        break;
    hql += " or ";
}
hql += " order by UpdateTimestamp";
ISession session = _sessionManager.OpenSession();
IQuery q = session.CreateQuery(hql);
return q.List<Recipe>();


which renders the following hql:

Code:
from Recipe where Title LIKE '%Warm Butternut Squash And Goat''s Cheese Salad%' order by UpdateTimestamp


The thing to notice is that I am replacing single quotes with double single quotes, e.g. Goat's becomes Goat's.

The problem is I am not getting any results back. The more curious thing is that it generates the following sql which actually returns a result set of 1:

Code:
select recipe0_.Uid as Uid23_, recipe0_.updatetimestamp as updateti2_23_, recipe0_.NumberOfImages as NumberOf3_23_, recipe0_.Title as Title23_, recipe0_.RecipeId as RecipeId23_, recipe0_.Summary as Summary23_, recipe0_.PublishedDate as Publishe7_23_, recipe0_.Visits as Visits23_, recipe0_.TempUid as TempUid23_, recipe0_.SMSIngredients as SMSIngr10_23_, recipe0_.GoLiveDate as GoLiveDate23_, recipe0_.RatingsTotal as Ratings12_23_, recipe0_.UsersTotal as UsersTotal23_ from Recipes recipe0_ where (Title LIKE '%Warm Butternut Squash And Goat''s Cheese Salad%') order by  UpdateTimestamp


I am very confused. How come the List<> method returns a count of 0 but the generated sql returns a count of 1?

Thanks

Paul


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.