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: HQL query is mangled
PostPosted: Wed May 31, 2006 8:59 am 
Newbie

Joined: Wed May 31, 2006 8:51 am
Posts: 4
Hi!

A have a problem with some HQL queries that puzzles me somewhat.

For example, when I execute this (using ISession.CreateQuery()):

Code:
FROM Grupp obj WHERE  ( (obj.Namn = 'Foo, Bar, Grupp 1 test') )


I get this error:

Quote:
Incorrect query syntax [FROM ActiveSolution.Lernia.Butler.EducationEntities.Grupp obj WHERE ( (obj.Namn = 'Foo, Bar, ActiveSolution.Lernia.Butler.EducationEntities.Grupp 1 test') ) ]


Apparently NHibernate confuses the string constant with the entity class name. This does not happen if I have no commas in the string, or if I change the case of the string.

If anyone have a suggestion of how to avoid this problem, I'm all ears!

I use NHibernate 1.0.1 and SQL Server 2000.

Thanks in advance,

Emil


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 31, 2006 10:16 am 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
Use parameters instead of embedded string literals.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 01, 2006 4:49 am 
Newbie

Joined: Wed May 31, 2006 8:51 am
Posts: 4
Sergey,

Thanks for the tip. Named parameters is an alternative, but the problem is that our HQL is dynamically created from object property values by several different functions. The final HQL query is composed of several different HQL-fragments, and it's more difficult to do that composition using named parameter queries.

If named parameters is the only way to go, I suppose we could do two passes, first create the query string with named parameters and then in a second pass set the values.

Or is it allowed to set the the IQuery.QueryString property after the query is created? That would simplify the algorithm, but these changes will still incur a lot of work for us.

The best would be if there was a way of escaping string literals in HQL so that NHibernates keeps its hands off them. But no such luck, then?

/Emil


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 01, 2006 7:56 am 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
This is a limitation of the HQL parser in NHibernate, and it is a piece of code that is hard to improve so I won't touch it. So yes, either named or positional parameters are the only way to go for now.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 01, 2006 8:02 am 
Newbie

Joined: Wed May 31, 2006 8:51 am
Posts: 4
Sergey,

Okay, thanks for clarifying. I'll fix this temporarily by converting all string literals to lower case before inserting them in query strings, which works because our entity class names always contain at least one upper case character, and string comparisons are case insensitive in our database.

It's a hack, but it's quick to implement :-)

/Emil


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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.