I've got a Criteria Query for a social networking site. A Person object has a collection of Friends (also person objects). The query grabs the first N friends, but I also want to eager load an associated object MainProfileImage and then a subsequent associated object MediumThumbnail.
I can do this in HQL easily:
select friends from Person person inner join person.Friends friends inner join fetch friends.MainProfileImage image inner join fetch image.MediumThumbnail where person = :person1 order by friends.LatestLogin desc
Here's My Criteria effort. For some reason this doesn't return anything!
public static IList<Person> GetFriends(Person person, int count)
{
Person personAlias = null;
Person friendAlias = null;
ICriteria criteria = NHibernateSessionManager.Instance.GetSession()
.CreateCriteria(typeof (Person), () => personAlias)
.CreateCriteria(() => personAlias.Friends, () => friendAlias, JoinType.LeftOuterJoin)
.CreateCriteria(() => friendAlias.MainProfileImage, JoinType.InnerJoin)
.CreateCriteria(() => friendAlias.MainProfileImage.MediumThumbnail, JoinType.InnerJoin)
.AddOrder(() => personAlias.LatestLogin, Order.Desc)
.Add<Person>(p => p.ID == person.ID)
.SetMaxResults(count);
return criteria.List<Person>();
}