NHibernate : Query by example on primary key produces "WHERE (1=1)"

标签 nhibernate

我有一个实体客户

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=927063http://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/

相关文章:

nhibernate - NHibernate 查询缓存过期

.net - NHibernate Linq 查询比 HQL 慢 3 倍

vb.net - 实现 NHibernate 单元测试以使用 VB.NET/MBUnit 生成模式

.net - nhibernate复杂投影表达

c# - NHibernate - 延迟加载原始类型

mysql - 如何通过 nhibernate/fluent nHibernate 使用 MYSQL BIT_COUNT 函数?

nhibernate - 使用 NHibernate 逐出和更新对象会使所有引用为空

c# - 我将如何对这个 nhibernate 查询进行单元测试?

.net - 如何在新数据库中创建 hibernate_unique_key 表?

events - NHibernate IPostInsertEventListener : Insert executed multiple times