简体   繁体   English

NHibernate多对多标准

[英]NHibernate Many-To-Many Criteria

I have a many to many relationship between Candidates and Positions. 我与候选人和职位之间有很多关系。 I am trying to limit the list of positions fetched to as follows 我试图将获取的职位列表限制如下

ICriteria criteria = this.GetSession().CreateCriteria(typeof(Candidate), "c");
criteria.CreateAlias("c.Positions", "plist",NHibernate.SqlCommand.JoinType.InnerJoin);
criteria.CreateAlias("plist.items", "p",NHibernate.SqlCommand.JoinType.InnerJoin);
criteria.Add(Expression.And
                        (
                            Expression.Eq("c.CandidateID", candidateID),
                            Expression.Eq("p.PositionID", positionID)
                        ));

However, all the positions are being fetched in the list. 但是,所有位置都在列表中。 What could be the reason? 可能是什么原因?

Can I just clarify your requirements? 我可以澄清您的要求吗? Are you saying that you want to return the particular candidate, and have the Candidate's Positions collection only to contain the Position with the specified positionID? 您是在说要返回特定的候选人,并且使候选人的职位集合仅包含具有指定positionID的职位吗?

If so, I don't think it works like that. 如果是这样,我认为它不是那样工作的。 You are specifying that you want to retrieve a Candidate with the specified ID that is associated with a Position with the specified ID. 您指定要检索与具有指定ID的职位相关联的具有指定ID的候选人。 Once the Candidate is loaded it will then load (lazily or otherwise) all of that candidate's associated Positions. 一旦候选人被加载,它将随后(延迟或其他方式)加载该候选人的所有相关职位。 That is correct behaviour since a candidate should always have access to all it's associated objects. 这是正确的行为,因为候选人应始终有权访问其所有关联的对象。

I think what you need to do is load the candidate and then search the Positions collection for the Position with the ID that you require. 我认为您需要做的是加载候选人,然后在Positions集合中搜索具有所需ID的Position。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM