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

帅比萌擦擦* 提交于 2020-01-31 18:00:52

问题


I have an entity Customer

public class Customer
{
    public virtual int ID { get; set; }
    public virtual string Firstname { get; set; }
    public virtual string Lastname { get; set; }
}

and my DAL method is :

    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);

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

P.S. I've forgotten to mention the version of NHibernate that I'm using. It's 2.0.1.GA.


回答1:


"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.




回答2:


What if you use EnableLike() on your query by example criteria object to query for a part of primary key + some other properties then your query would return more than 1 record. e.g.: User name for a user is primary key. Your object's properties including primary key are set to values which should be queried by like operator. In that case NHibernate leaves no choice other than writing your own query method.




回答3:


I know this is an old post, but I ran across this problem today and none of the answers here helped. We had a method called CountByExample() that works correctly as long as the ID column isn't specified in the example.

For our scenario, the count is only valid if an object with the specified ID exists with a specific set of parameters. The object is a rather large object so loading it would be an unnecessary waste of resources and bandwith. Because NHibernate strips the ID from the example, the count was always incorrect.

I solved this with the following tweak that works fine for me.

            criteria.Add(example);
            //HACK: Check for ID query and force NHibernate to take it
            var prop = searchParameters.GetType().GetProperty("ID");
            if (prop != null)
            {
                criteria.Add(Restrictions.Eq("ID", prop.GetValue(exampleT)));
            }

Where "searchParameters" is an object like:

var searchParameters= new { ID = 48, isEnabledForAds = true, hasOptedOut = false};


来源:https://stackoverflow.com/questions/424699/nhibernate-query-by-example-on-primary-key-produces-where-1-1

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!