我将 NH 3.3.1.4000 与 Oracle 10g 和 SQL Server 2008 一起使用。
我最近遇到了这个可能的错误。考虑以下两个查询:
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>();
上述任一查询都会产生以下 SQL 查询:
select content0_.Id as Id6_,
content0_.Name as Name6_,
from Content content0_
where content0_.Name = ''
任何传入并带有“-”(连字符)的参数都不会出现在实际的 SQL 查询中
如果我删除参数中的 .Cacheable 或 SetCacheable() 或“-”,它可以正常工作。在我开始查看 NH 源代码之前,有什么解决方法的想法吗?谢谢。
更新 我可以确认 SQL Server 也存在同样的问题。我猜它与数据库无关,而与查询缓存无关。我应该提到第一次运行查询时,在缓存发生之前,生成的查询是正确的。但是当我第二次运行相同的查询时,这次从查询缓存中加载,可缓存连字符不起作用。请参阅我在 .Net MVC 应用程序上使用 SQL Server 2008 运行的以下测试。
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();
我第一次加载页面时生成了以下查询并且都是正确的:
-- 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'
但是在我重新加载页面并让查询缓存做它的事情后,我得到以下三个查询:
-- 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'
如您所见,第二次语句#1 中的参数为空。语句#1 和语句#2 之间的唯一区别是查询缓存的使用。只要参数中没有“-”连字符,缓存查询在第二次加载(test3)中工作正常。