我开始注意到 @ManyToOne
映射的默认 FetchType
在 JPA 和 Hibernate 中是 EAGER
,而对于 @OneToMany
映射,默认FetchType
为LAZY
。
这背后的具体原因是什么?
最佳答案
根据 JPA 2.0 规范c,默认值如下:
OneToMany: LAZY
ManyToOne: EAGER
ManyToMany: LAZY
OneToOne: EAGER
在 hibernate 中,一切都是懒惰的
来自 Hibernate 文档,
By default, Hibernate uses lazy select fetching for collections and lazy proxy fetching for single-valued associations. These defaults make sense for most associations in the majority of applications.
为了回答您的问题,Hibernate 是 JPA 标准的一个实现。 Hibernate 有它自己的操作怪癖,但根据 Hibernate 文档
By default, Hibernate uses lazy select fetching for collections and lazy proxy fetching for single-valued associations. These defaults make sense for most associations in the majority of applications.
所以 Hibernate 将始终使用延迟获取策略加载任何对象,无论您声明了哪种类型的关系。
JPA 规范 假设通常大多数应用程序都要求单例关系默认是急切的,而多值关系默认是惰性的。
引用here更多
关于java - 为什么 JPA 对于 @ManyToOne 关系默认使用 FetchType EAGER,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27519399/