为什么Hibernate在FetchType.Eager映射中查找子项时加载父实体 [英] Why does Hibernate load the parent entity when finding a child in FetchType.Eager mapping

查看:82
本文介绍了为什么Hibernate在FetchType.Eager映射中查找子项时加载父实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用:Hibernate 4



在有FetchType.Eager映射的父子关系中,我注意到Hibernate随时会加载父实体和EntityManager。查找是在一个孩子上进行的。这似乎有点浪费。父级的幕后加载会导致持久化上下文中出现额外的SELECT和额外对象。此外,它强制应用程序解决亲子关系时删除孩子



为什么Hibernate的行为如此?这是由JPA规范规定的吗?

解决方案

它来自规范,当实体需要获取持久属性和渴望获取策略时被加载:


如果所有属性都带有
FetchType.EAGER - 无论明确指定还是默认 - (包括
关系和其他集合值属性)已从数据库加载
或由应用程序分配。带有
的属性FetchType.LAZY可能已经加载,也可能未加载。



Using: Hibernate 4

In a parent-child relationship where there's a FetchType.Eager mapping, I noticed that Hibernate will load the parent entity any time and EntityManager.find is performed on a child. This seems a bit wasteful. The "behind the scene" loading of the parent is resulting in an extra SELECT and extra object in the persistence context. Also, it forces the app to address the parent-child association when removing a child.

Why does Hibernate behave this way? Is this mandated by the JPA spec?

解决方案

It comes from the specification that persistent attribute with eager fetch strategy must be fetched when entity is loaded:

An entity is considered to be loaded if all attributes with FetchType.EAGER—whether explictly specified or by default—(including relationship and other collection-valued attributes) have been loaded from the database or assigned by the application. Attributes with FetchType.LAZY may or may not have been loaded.

这篇关于为什么Hibernate在FetchType.Eager映射中查找子项时加载父实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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