Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you work with detached QueryOver instances?

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

like image 432
Remi Despres-Smyth Avatar asked Sep 08 '11 19:09

Remi Despres-Smyth


1 Answers

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?

like image 151
Daniel Schilling Avatar answered Oct 12 '22 00:10

Daniel Schilling