2009-01-08 71 views
4

我有一个实体的客户NHibernate的:通过例如在主键查询产生 “WHERE(1 = 1)”

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? 我在做什么错?

P.S.我忘记提及我使用的NHibernate版本。它是2.0.1.GA.

回答

0

如果在示例条件对象的查询中使用EnableLike()来查询主键的一部分+其他属性,那么您的查询将返回多于一条记录。例如:用户的用户名是主键。您的对象的属性(包括主键)被设置为应该由like运算符查询的值。在这种情况下,除了编写自己的查询方法之外,NHibernate别无选择。