2

I want to select all requests that are outstanding for a given manager. A manager can have multiple teams.

I compose the queries applying various restrictions based upon permissions, and alter the queries to provide row counts, existence checks, sub queries, etc.

The composition makes use of QueryOver, though using ICriteria instead would also be acceptable.

Given the following classes;

class Team {
    public virtual int Manager { get; set; }
    public virtual ISet<int> Members { get; set; }
}

class Request {
    public virtual int Owner { get; set; }
    public virtual bool IsOutstanding { get; set; }
}

class static SomeRestrictions {
    public static void TeamsForManager<TRoot> (this IQueryOver<TRoot, Team> query, int managerId) {
        // In reality this is a little more complex
        query.Where (x => x.Manager == managerId);
    }
}

This is the current query that I'm trying (which doesn't work).

var users = QueryOver.Of<Team> ();
users.TeamsForManager (5)
users.Select (/* not sure */);

var requests = session.QueryOver<Request> ()
    .Where (x => x.IsOutstanding)
    .WithSubquery.WhereProperty (x => x.Owner).In (users);

The HQL to select the users would be:

"SELECT m FROM Team t JOIN t.Members m WHERE <TeamsForManager restrictions>"

But I don't want to use HQL because I can't then compose it with other restrictions based upon permissions. I also wouldn't be able to compose it with other queries to turn it into row counts/existence checks, etc.

Endy Tjahjono
  • 24,120
  • 23
  • 83
  • 123
Chris Chilvers
  • 6,429
  • 3
  • 32
  • 53
  • i dont fully understand the question but maybe `users.Select (Projections.Id());` is what you need? – Firo Sep 22 '11 at 10:50
  • Trouble is, that would select the team's id, when I'm after the element's value. It doesn't seem possible at the moment, so instead I've changed the domain model. – Chris Chilvers Sep 22 '11 at 13:31

1 Answers1

1

i saw you changed the model but this would have been the way

var users = QueryOver.Of<Team> ();
users.TeamsForManager (5);
users.JoinAlias(t => t.Members, () => membervalue).Select(() => membervalue);
Firo
  • 30,626
  • 4
  • 55
  • 94
  • the problem is, Members is an ISet, there is no id. Its a collection of primitive elements – Chris Chilvers Sep 22 '11 at 14:00
  • Unfortunately that doesn't work either. Looks like the issue was only recently resolved in Hibernate itself HHH-3646 https://hibernate.onjira.com/browse/HHH-3646 . Hopefully this change will make its way into NHibernate soon. – Chris Chilvers Dec 13 '11 at 12:59