views:

173

answers:

2

Hi,

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 may be the reason?

Thanks

Anurag

A: 

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?

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

Richard Bramley
A: 

Yes I want to return a particular candidate, and have the Candidate's Positions collection only to contain the Position with the specified positionID.

Can you point to some documentation on this i.e. the fact that once a candidate is loaded, all the associate positions will be loaded.

Thanks

Anurag

Anurag