Fluent nHibernate 集合的选择性加载
我只是想知道在加载包含集合的实体(例如可能包含 0 -> 的 Post)时是否会发生这种情况。 n 评论(如果您可以定义要返回的评论数量)。
目前我有这个:
public IList<Post> GetNPostsWithNCommentsAndCreator(int numOfPosts, int numOfComments)
{
var posts = Session.Query<Post>().OrderByDescending(x => x.CreationDateTime)
.Take(numOfPosts)
.Fetch(z => z.Comments)
.Fetch(z => z.Creator).ToList();
ReleaseCurrentSession();
return posts;
}
有没有一种方法可以添加“跳过”和“评论”以允许对集合进行某种分页功能,这样您就不会最终加载很多不需要的东西。
我知道延迟加载,但我真的不想使用它,我正在使用 MVC 模式,并希望我的对象从加载的存储库返回,以便我可以缓存它们。我真的不希望我的观点导致 select 语句。
解决这个问题的唯一真正方法是不对评论执行提取,而是对评论执行单独的选择以按创建日期时间排序,然后选择前 5 个,然后将返回的结果放入 Post 对象中?
对此的任何想法/链接将不胜感激。
谢谢,
乔恩
I was just wondering whether when loading an entity which contains a collection e.g. a Post which may contain 0 -> n Comments if you can define how many comments to return.
At the moment I have this:
public IList<Post> GetNPostsWithNCommentsAndCreator(int numOfPosts, int numOfComments)
{
var posts = Session.Query<Post>().OrderByDescending(x => x.CreationDateTime)
.Take(numOfPosts)
.Fetch(z => z.Comments)
.Fetch(z => z.Creator).ToList();
ReleaseCurrentSession();
return posts;
}
Is there a way of adding a Skip and Take to Comments to allow a kind of paging functionality on the collection so you don't end up loading lots of things you don't need.
I'm aware of lazy loading but I don't really want to use it, I'm using the MVC pattern and want my object to return from the repositories loaded so I can then cache them. I don't really want my views causing select statements.
Is the only real way around this is to not perform a fetch on comments but to perform a separate Select on Comments to Order By Created Date Time and then Select the top 5 for example and then place the returned result into the Post object?
Any thoughts / links on this would be appreciated.
Thanks,
Jon
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
简单的获取在关联的表上执行左外连接,以便它可以用数据混合集合实体。您想要做的事情将需要对特定实体进行单独的查询。从那里您可以使用任意数量的构造来限制您的结果集(skip/take、setmaxresults 等)
A fetch simple does a left-outer join on the associated table so that it can hydrate the collection entities with data. What you are looking to do will require a separate query on the specific entities. From there you can use any number of constructs to limit your result set (skip/take, setmaxresults, etc)