JPA / hibernate大集合 [英] JPA/hibernate big collections

查看:117
本文介绍了JPA / hibernate大集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在包含两种类型的实体(父项和子项)的情况下:


- @OneToMany收集子项;

默认情况下是对子集合进行延迟加载。这种模式适用于少数儿童,但如果数量变得非常大,这似乎难以为继。因此,对于那些我认为孩子数量非常大的场合,我已经使用了带分页的服务方法(如getChildren(Parent parent,int offset,int count))。

问题是:这是处理这种情况的最佳方式吗?或者我错过了什么?



感谢,
Piotr

解决方案


这个模型适用于少量的孩子,但是如果数量变得非常大,这看起来难以为继。

我会说这一切都取决于你想要对他们做什么,但在大多数情况下,这是真的。


因此,对于我认为孩子数量非常大的场合,我使用了带分页的服务方法(比如getChildren(Parent parent,int offset,int count))。
blockquote>

如果您需要显示(可能非常大)的浏览结果,则寻呼是一种非常自然的方法。人类通常不需要或不需要所有记录,无论如何他们不能处理大量的结果。应用程序必须一次处理所有结果的情况当然不同,但JPA可能根本不适合他们。


问题是:这是处理这种情况的最佳方式吗?或者我错过了什么?


海事组织,它肯定比提供整个集合的结果页好多了通过调用 parent.getChildren()来获取,它将保存一些数据库,网络和应用服务器资源。

您可能会考虑的另一件事是限制搜索结果的最大数量。而不是分页10⁶的结果(谁会去浏览这个结果?),这是很常见的(至少对我的经验来说)要求用户执行更严格的搜索,即添加搜索条件,直到结果的数量变得可人为管理。这与你最初的问题有点不同。


In a scenario with two types of entities, Parent and Child:

Parent - @OneToMany Collection children;

The default is to have lazy loading on the collection of children. This model works great for small numbers of children, but if the number grows very large this seems unsustainable. So for occasions where I think the number of children will be very large I have used service methods with paging (like "getChildren(Parent parent, int offset, int count)") instead.

Question is: is this the best way to handle situations like this? Or have I missed something?

Thanks, Piotr

解决方案

This model works great for small numbers of children, but if the number grows very large this seems unsustainable.

I'd say that it all depends on what you want to do with them but in most cases, this is true.

So for occasions where I think the number of children will be very large I have used service methods with paging (like "getChildren(Parent parent, int offset, int count)") instead.

Paging is a very natural approach if you need to display a (potentially very) large number of results for browsing. Humans most often don't want or need all records and they can't deal with huge numbers of results anyway. The case of applications having to deal with all results at once is of course different but JPA might simply not be appropriate for them.

Question is: is this the best way to handle situations like this? Or have I missed something?

IMO, it is definitely much better than feeding a result page with the the whole collection that you'd get by calling parent.getChildren(), and it will save some database, network, app server resources.

Another thing you might consider is restricting the max number of results when doing a search. Instead of paging 10⁶ results (who is going to browse that anyway?), it's common (at least to my experience) to ask the user to perform a more restrictive search i.e. to add search criteria until the number of results becomes human manageable. This is a bit different from your initial question though.

这篇关于JPA / hibernate大集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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