NHibernate Lambda Extensions - Eager Loading a collection's assosciations

青春壹個敷衍的年華 提交于 2019-12-04 21:06:17

I believe that the order of your statements causes the query to generate the not desired SQL you are getting (hence no results). Here is how it should be:

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)
        .Add<Person>(p => p.ID == person.ID)
        .CreateCriteria(() => personAlias.MainProfileImage, JoinType.InnerJoin)
        .CreateCriteria(() => personAlias.MainProfileImage.MediumThumbnail, JoinType.InnerJoin)
        .AddOrder(() => personAlias.LatestLogin, Order.Desc)
        .SetMaxResults(count);
    return criteria.List<Person>();
}

Furthermore by your description it is not clear from which association you want to get the MainProfileImage and MediumThumbnail data from. As you are using it in your Criteria statement you are asking the data to come from the main Person object whose friends you are getting as you are using the friendsAlias. I have changed it to use the personAlias instead as I believe this is where you mean to associate the data from.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!