在具有两种类型实体的场景中,父实体和子实体:
parent - @OneToMany 集合子项;
默认是对子集合进行延迟加载。这种模式对少数 child 非常有效,但如果数量增长非常大,这似乎是不可持续的。因此,在我认为子级数量会非常大的情况下,我会使用带分页的服务方法(例如“getChildren(Parent parent, int offset, int count)”)。
问题是:这是处理这种情况的最佳方式吗?还是我错过了什么?
谢谢, 彼得
最佳答案
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.
如果您需要显示(可能非常)大量的结果以供浏览,则分页是一种非常自然的方法。人们通常不想要或不需要所有记录,而且他们无论如何也无法处理大量结果。应用程序必须一次处理所有结果的情况当然是不同的,但 JPA 可能根本不适合它们。
Question is: is this the best way to handle situations like this? Or have I missed something?
IMO,这绝对比通过调用 parent.getChildren()
将整个集合提供给结果页面要好得多,它会节省一些数据库、网络、应用程序服务器资源。
您可能会考虑的另一件事是在进行搜索时限制最大结果数。不是分页 10⁶ 个结果(无论如何谁会浏览那个?),通常(至少根据我的经验)要求用户执行更严格的搜索,即添加搜索条件,直到结果的数量变得可控为止。不过,这与您最初的问题有点不同。
关于java - JPA/hibernate 大集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3783301/