I came to notice that the default FetchType
for the @ManyToOne
mapping is EAGER
in JPA and Hibernate, whereas for the @OneToMany
mapping, the默认 FetchType
是 LAZY
。
这背后的具体原因是什么?
原文由 vatsal mevada 发布,翻译遵循 CC BY-SA 4.0 许可协议
I came to notice that the default FetchType
for the @ManyToOne
mapping is EAGER
in JPA and Hibernate, whereas for the @OneToMany
mapping, the默认 FetchType
是 LAZY
。
这背后的具体原因是什么?
原文由 vatsal mevada 发布,翻译遵循 CC BY-SA 4.0 许可协议
15 回答8.4k 阅读
8 回答6.2k 阅读
1 回答4k 阅读✓ 已解决
3 回答6k 阅读
3 回答2.2k 阅读✓ 已解决
2 回答3.1k 阅读
2 回答3.8k 阅读
在 JPA 2.0 规范 中,默认值如下所示:
在 休眠状态下,一切都是懒惰的
从休眠文档,
为了回答您的问题,Hibernate 是 JPA 标准的实现。 Hibernate 有它自己的操作怪癖,但根据 Hibernate 文档
所以 Hibernate 将始终使用延迟获取策略加载任何对象,无论您声明了哪种类型的关系。
JPA 规范 假设一般情况下,大多数应用程序将要求单例关系默认是急切的,而多值关系默认是惰性的。
参考 这里 了解更多