NHibernate:通过示例查询主键会产生"WHERE(1 = 1)". [英] NHibernate : Query by example on primary key produces "WHERE (1=1)"

查看:124
本文介绍了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;
    }

但是问题是,当我这样调用我的方法时

but the problem is that when I call my method like this

    var exemple = new Customer() { ID = 2 };
    var customers = provider.GetCustomers(exemple);

由于NHibernate生成以下SQL查询,因此我在数据库中拥有所有客户的集合

I have a collection of all my customers in the database because NHibernate generates the following SQL query

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 supports QBE on primary key ? What am I doing wrong ?

P.S.我忘了提到我正在使用的NHibernate版本.是2.0.1.GA.

P.S. I've forgotten to mention the version of NHibernate that I'm using. It's 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.

"The ID is ignored when using query by example. This is done because an example object with the id set would return only one object anyways." - http://forum.hibernate.org/viewtopic.php?t=927063 and http://forum.hibernate.org/viewtopic.php?p=2351666&sid=c22d2c37f8d67e268b6ffe547f57ad9e
This is for Hibernate. NHibernate was ported from it. So I'm sure that this is by design in NHibernate too. So use Get instead of QBE on id.

这篇关于NHibernate:通过示例查询主键会产生"WHERE(1 = 1)".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆