Nhibernate - stuck with detached criteria (asp.net mvc 1 with nhibernate 2) c#

Posted by Jen on Stack Overflow See other posts from Stack Overflow or by Jen
Published on 2011-02-25T05:51:50Z Indexed on 2011/02/27 23:25 UTC
Read the original article Hit count: 283

OK so I can't find a good example of this so I can better understand how to use detached criteria (assuming that's what I want to use in the first place).

I have 2 tables. Placement and PlacementSupervisor

My PlacementSupervisor table has a FK of PlacementID which relates to Placement.PlacementID - though my nhibernate model class has PlacementSupervisor . Placement (rather than specifically specifying a property of placement ID - not sure if this is important).

What I am trying to do is - if values are passed through for the supervisor ID I want to restrict placements with that supervisor id.

Have tried:

ICriteria query = m_PlacementRepository.QueryAlias("p")
....
    if (criteria.SupervisorId > 0 && !string.IsNullOrEmpty(criteria.SupervisorTypeId))
                {

                    DetachedCriteria entityQuery = DetachedCriteria.For<PlacementSupervisor>("sup")
                         .Add(Restrictions.And(
                                       Restrictions.Eq("sup.supervisorId", criteria.SupervisorId),
                                      Restrictions.Eq("sup.supervisorTypeId", criteria.SupervisorTypeId)
                                   ))
                    .SetProjection(Projections.ProjectionList()
                                       .AddPropertyAlias("Placement.PlacementId", "PlacementId")
                                       );
                    query.Add(Subqueries.PropertyIn("p.PlacementId", entityQuery));
                }

Which just gives me the error: Could not find a matching criteria info provider to: (sup.supervisorId = 5 and sup.supervisorTypeId = U)

Firstly supervisorTypeId is a string. Secondly I don't understand how to achieve what I'm trying to do so have just been trying various combinations of projections, and property aliases and subquery options..as I don't get how I'm supposed to join to another table/entity when the FK key sits in the second table.

Can someone point me in the right direction. It seems like such an easy thing to do from a data perspective that hopefully I'm just missing something obvious!!

© Stack Overflow or respective owner

Related posts about c#

Related posts about nhibernate