我有一个社交网站的标准查询。一个 Person 对象有一个 Friends 集合(也是 person 对象)。查询抓取了前 N 个好友,但我也想急切加载关联对象 MainProfileImage,然后加载后续关联对象 MediumThumbnail。
我可以很容易地在 HQL 中做到这一点:
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
这是我的标准努力。由于某种原因,这不会返回任何东西!
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>();
}