How do you work with detached QueryOver instances?
Asked Answered
M

1

8

This NHibernate blog entry notes how detached QueryOver queries (analogous to DetachedCriteria) can be created (using QueryOver.Of<T>()). However, looking this over, it doesn't look analogous to me at all.

With DetachedCriteria, I would create my instance and set it up however I need, and afterwards call GetExecutableCriteria() to then assign the session and execute the query. With the "detached" QueryOver, most of the API is unavailable (ie, to add restrictions, joins, ordering, etc...) until I call GetExecutableQueryOver, which requires takes an ISession or IStatelessSession, at which point you are no longer disconnected.

How do you work with detached QueryOver instances?

EDIT:

Actual problem was related to how I'm storing the detached QueryOver instance:

public class CriteriaQuery<T>
{
    internal protected QueryOver<T> _QueryOver { get; set; }

    public CriteriaQuery()
    {
        _QueryOver = QueryOver.Of<T>();
    }

    // Snip
}

It should be a QueryOver<T, T>.

Medlar answered 8/9, 2011 at 19:9 Comment(2)
What was the actual problem you mention below?Bluhm
QueryOver<T> doesn't have the API I was expecting; QueryOver<T, T> is what I needed.Medlar
H
13

I'm using NHibernate 3.1.0.4000. The following code compiles successfully:

Employee salesRepAlias = null;

var query = QueryOver.Of<Customer>()
    .JoinAlias(x => x.SalesRep, () => salesRepAlias)
    .Where(x => x.LastName == "Smith")
    .Where(() => salesRepAlias.Office.Id == 23)
    .OrderBy(x => x.LastName).Asc
    .ThenBy(x => x.FirstName).Asc;

return query.GetExecutableQueryOver(session)
    .List();

This illustrates using restrictions, joins, and ordering on a detached QueryOver just like you would with a regular one.

Could you please post the code that demonstrates the API features that are unavailable?

Hegelian answered 8/9, 2011 at 19:32 Comment(1)
Selected your answer and added the actual problem to the question, cause your post pushed me towards the problem. Thanks.Medlar

© 2022 - 2024 — McMap. All rights reserved.