NHibernate Lambda扩展-渴望加载集合的关联 [英] NHibernate Lambda Extensions - Eager Loading a collection's assosciations

查看:148
本文介绍了NHibernate Lambda扩展-渴望加载集合的关联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个针对社交网站的条件查询.一个Person对象具有Friends的集合(也包括person对象).该查询捕获了前N个朋友,但我也想急于加载关联的对象MainProfileImage,然后再加载后续的关联对象MediumThumbnail.

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.

我可以在HQL中轻松做到这一点:

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>();
}

推荐答案

我相信您的语句顺序会导致查询生成您正在获取的不需要的SQL(因此没有结果). 这应该是这样的:

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>();
}

此外,根据您的描述,您还不清楚要从哪个关联中获取MainProfileImage和MediumThumbnail数据.当您在Criteria语句中使用它时,您正在要求数据来自主要的Person对象,就像使用friendsAlias时所遇到的朋友一样.我将其更改为使用personAlias,因为我认为这是您要关联数据的地方.

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.

这篇关于NHibernate Lambda扩展-渴望加载集合的关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆