开发者

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

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 su开发者_开发技巧pervisor 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!!


This might help. It's a good overview of the criteria api by Fabio Maulo

Generally you use DeteachedCriterias if you don't want to immediately use it in a session, so it doesn't sound like you really need one.

The above link (section 13.4) gives an example (which I've modified to fit your terms):

IList placements = sess.CreateCriteria(typeof(Placement))
                 .CreateAlias("PlacementSupervisor", "sup")
                 .Add( Expression.EqProperty("sup.supervisorId", criteria.SupervisorId") )
                 .Add( Expression.EqProperty("sup.supervisorTypeId", criteria.SupervisorTypeId) )
                 .List();

A few other notes:

  1. I wouldn't worry about FK's. As long as you've mapped the relationship, NH can figure out how to do the join.
  2. Property names are case sensitive I think so try it with "sup.SupervisorId" as well.
  3. Make sure you're using the property name and not the db column name.


I ended up being able to use the code from above modified.

  query.CreateCriteria("Supervisors")  
      .Add(Restrictions.Eq("SupervisorId", (int)criteria.SupervisorId))
      .Add(Restrictions.Eq("SupervisorType.SupervisorTypeId", criteria.SupervisorTypeId));

With Supervisors being a property on my Placement model class.

Note also that Expressions has been semi-deprecated by Restrictions.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜