NHibernate Lambda 扩展 - 热切加载集合的关联
我有一个社交网站的标准查询。一个 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>();
}
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>();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相信你的语句的顺序会导致查询生成你不想要的 SQL(因此没有结果)。
它应该是这样的:
此外,根据您的描述,不清楚您想要从哪个关联获取 MainProfileImage 和 MediumThumbnail 数据。当您在 Criteria 语句中使用它时,您要求数据来自主 Person 对象,当您使用
friendsAlias
时,您将获得该对象的朋友。我已将其更改为使用personAlias
,因为我相信这是您要关联数据的位置。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:
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 thepersonAlias
instead as I believe this is where you mean to associate the data from.