java - JPA/hibernate 大集合

标签 java hibernate spring orm jpa

在具有两种类型实体的场景中,父实体和子实体:

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/

相关文章:

java - NetBean 为 Hibernate 生成的 reveng.xml 不包含关联

java - Spring AMQP 的 "Cannot convert value of type"异常

java - Spring,SockJS - WebSocket 握手期间出错 : Unexpected response code 400

java - HttpResponse 使用 android 问题 : execute always causes exception?

Java:使用定界符摆脱大写字母单词?

java - 从文件创建扫描仪时抛出 FileNotFoundException

java - JsValue 中的 Scala 模式匹配类型

java - 如何在 hibernate 中保存之前获取自动生成的字段

java - 提取公共(public)列(审核字段)[Java][JPA]

java - AJAX/Spring MVC - 没有 Spring Security 的 403 禁止错误