我有一个实体客户
public class Customer
{
public virtual int ID { get; set; }
public virtual string Firstname { get; set; }
public virtual string Lastname { get; set; }
}
我的 DAL 方法是:
public IList<Customer> GetCustomers(Customer example)
{
var customers = default(IList<Customer>);
using (var sessiong = GetSession())
{
customers = sessiong.CreateCriteria(typeof(Customer))
.Add(Example.Create(example))
.List<Customer>();
}
return customers;
}
但问题是当我这样调用我的方法时
var exemple = new Customer() { ID = 2 };
var customers = provider.GetCustomers(exemple);
我在数据库中有我所有客户的集合,因为 NHibernate 生成以下 SQL 查询
NHibernate: SELECT this_.CustomerId as CustomerId0_0_, this_.Firstname as Firstname0_0_, this_.Lastname as Lastname0_0_ FROM Customers this_ WHERE (1=1)
NHibernate 在主键上支持 QBE 吗?
我究竟做错了什么 ?
附言我忘了提及我正在使用的 NHibernate 版本。这是 2.0.1.GA。
最佳答案
“使用示例查询时,ID 被忽略。这样做是因为带有 id 集的示例对象无论如何只会返回一个对象。” - http://forum.hibernate.org/viewtopic.php?t=927063和 http://forum.hibernate.org/viewtopic.php?p=2351666&sid=c22d2c37f8d67e268b6ffe547f57ad9e
这是用于休眠的。 NHibernate 就是从它移植过来的。所以我确信这也是 NHibernate 的设计。所以在 id 上使用 Get 而不是 QBE。
关于NHibernate : Query by example on primary key produces "WHERE (1=1)",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/424699/