NHibernate Query cache bug with hyphens

279 Views Asked by At

I am using NH 3.3.1.4000 with Oracle 10g and SQL Server 2008.

I ran into this possible bug recently. Consider the following two queries:

var query1 = session.Query<Content>().Cacheable()
                    .Where(c => c.Name == "test-test").ToList();

var query2 = session.CreateQuery("from Content c where c.Name = :name")
                    .SetString("name", "test-test")
                    .SetCacheable(true); 
var list = query.List<Content>();

Either one of the above queries will produce the following SQL query:

select content0_.Id       as Id6_,      
content0_.Name   as Name6_,      
from   Content content0_
where  content0_.Name = ''

Any parameter that is passed in and has a '-' (hyphen), doesn't not make it to the actual SQL query

If I remove the .Cacheable or SetCacheable() or the "-" in the parameter, it works correctly. Any ideas for workaround before I start looking in to the NH source code? Thanks.

UPDATE I can confirm that the same issue exists for SQL Server also. I am guessing it has nothing to do with the database rather with the query cache. I should have mentioned that the first time the query runs, before caching takes place, the query that is generated is correct. But when I run the same query the second time, this time loading from query cache, cacheable with hyphens doesn't work. Please see the following test I ran using SQL Server 2008 on a .Net MVC app.

var test1 = session.Query<Student>().Cacheable().Where(c => c.Firstname == "first-name").ToList();

var test2 = session.Query<Student>().Where(c => c.Firstname == "first-name").ToList();

var test3 = session.Query<Student>().Cacheable().Where(c => c.Firstname == "firstname").ToList();

The first time I loaded the page the following queries were generated and were all correct:

-- statement #1
select student0_.Id        as Id1_,
   student0_.Firstname as Firstname1_,
   student0_.Lastname  as Lastname1_
from   Students student0_
where  student0_.Firstname = 'first-name'

-- statement #2
select student0_.Id        as Id1_,
   student0_.Firstname as Firstname1_,
   student0_.Lastname  as Lastname1_
from   Students student0_
where  student0_.Firstname = 'first-name'

-- statement #3
select student0_.Id        as Id1_,
   student0_.Firstname as Firstname1_,
   student0_.Lastname  as Lastname1_
from   Students student0_
where  student0_.Firstname = 'firstname'

But after I reload the page, and let Query Cache do its thing, I get the following three queries:

-- statement #1
Cached query: 
select student0_.Id        as Id1_,
   student0_.Firstname as Firstname1_,
   student0_.Lastname  as Lastname1_
from   Students student0_
where  student0_.Firstname = ''

-- statement #2
select student0_.Id        as Id1_,
   student0_.Firstname as Firstname1_,
   student0_.Lastname  as Lastname1_
from   Students student0_
where  student0_.Firstname = 'first-name'

-- statement #3
Cached query: 
select student0_.Id        as Id1_,
   student0_.Firstname as Firstname1_,
   student0_.Lastname  as Lastname1_
from   Students student0_
where  student0_.Firstname = 'firstname'

As you can see, the parameter is empty in statement #1 the second time. The only difference between statement #1 and statement #2 is the use of query cache. Cached query works fine in the second load (test3) as long as there is no "-" hyphen in the parameter.

1

There are 1 best solutions below

0
On

It seems this must be a bug in NHibernate Profiler rather than with NHibernate or the query cache. I have been able to reproduce your problem with parameters including hyphens for cached queries being displayed as empty strings in the profiler.

I have always gotten correct result sets in my tests, though. Enabling debug level for logger NHibernate.Cache.StandardQueryCache also indicates that the query cache never sees these empty string parameters, but rather the actual parameter values with hyphens.

My conclusion is that the profiler displays these parameters incorrectly when listing a cached query.