gpt4 book ai didi

hibernate - 了解 Hibernate/JPA 对 findAll 和 findOne 的不同行为

转载 作者:行者123 更新时间:2023-12-01 05:04:56 24 4
gpt4 key购买 nike

我正在使用 findAll 方法(来自 Spring Data JPA 存储库)调试性能问题,问题是正在使用的提取计划。 (Hibernate 正在执行额外的 Selects 来检索额外的实体,而不是使用 fetch join)

我的实体与另一个实体有一些关系,我面临的问题与 *ToOne 关系有关。

在谷歌上花了一些时间后,我意识到有一些方法可以管理 Hibernate 将执行的查询(使用 JPA Criteria、NamedEntityGraph、自定义查询等)。

我创建了一个小例子来演示不同的行为,但我想 理解为什么 Hibernate 默认这样做 .我花了一些时间在文档中搜索,但找不到任何对此默认行为的引用。

https://github.com/pmvilaca/jpa-test

区别:

Hibernate: 
SELECT
user0_.id AS id1_1_0_,
userdetail1_.id AS id1_21_,
contactdet2_.id AS id1_0_2_,
user0_.contact_details_id AS contact_3_1_0_,
user0_.name AS name2_1_0_,
userdetail1_.nationality AS national2_2_1_,
userdetail1_.user_id AS user_id3_2_1_,
contactdet2_.email AS email2_0_2_,
contactdet2_.mobile AS mobile3_0_2_
FROM user user0_
LEFT OUTER JOIN user_details userdetail1_ ON user0_.id = userdetail1_.user_id
LEFT OUTER JOIN contact_details contactdet2_ ON user0_.contact_details_id = contactdet2_.id


Hibernate: select user0_.id as id1_1_, user0_.contact_details_id as contact_3_1_, user0_.name as name2_1_ from user user0_
Hibernate: select contactdet0_.id as id1_0_0_, contactdet0_.email as email2_0_0_, contactdet0_.mobile as mobile3_0_0_ from contact_details contactdet0_ where contactdet0_.id=?
Hibernate: select userdetail0_.id as id1_2_2_, userdetail0_.nationality as national2_2_2_, userdetail0_.user_id as user_id3_2_2_, user1_.id as id1_1_0_, user1_.contact_details_id as contact_3_1_0_, user1_.name as name2_1_0_, contactdet2_.id as id1_0_1_, contactdet2_.email as email2_0_1_, contactdet2_.mobile as mobile3_0_1_ from user_details userdetail0_ left outer join user user1_ on userdetail0_.user_id=user1_.id left outer join contact_details contactdet2_ on user1_.contact_details_id=contactdet2_.id where userdetail0_.user_id=?
Hibernate: select contactdet0_.id as id1_0_0_, contactdet0_.email as email2_0_0_, contactdet0_.mobile as mobile3_0_0_ from contact_details contactdet0_ where contactdet0_.id=?
Hibernate: select userdetail0_.id as id1_2_2_, userdetail0_.nationality as national2_2_2_, userdetail0_.user_id as user_id3_2_2_, user1_.id as id1_1_0_, user1_.contact_details_id as contact_3_1_0_, user1_.name as name2_1_0_, contactdet2_.id as id1_0_1_, contactdet2_.email as email2_0_1_, contactdet2_.mobile as mobile3_0_1_ from user_details userdetail0_ left outer join user user1_ on userdetail0_.user_id=user1_.id left outer join contact_details contactdet2_ on user1_.contact_details_id=contactdet2_.id where userdetail0_.user_id=?
Hibernate: select userdetail0_.id as id1_2_2_, userdetail0_.nationality as national2_2_2_, userdetail0_.user_id as user_id3_2_2_, user1_.id as id1_1_0_, user1_.contact_details_id as contact_3_1_0_, user1_.name as name2_1_0_, contactdet2_.id as id1_0_1_, contactdet2_.email as email2_0_1_, contactdet2_.mobile as mobile3_0_1_ from user_details userdetail0_ left outer join user user1_ on userdetail0_.user_id=user1_.id left outer join contact_details contactdet2_ on user1_.contact_details_id=contactdet2_.id where userdetail0_.user_id=?

任何的想法?

谢谢

最佳答案

@OneToOne 的默认获取类型是 FetchType.EAGER .
因此,在没有任何关于如何优化查询的提示的情况下,Hibernate 将遵循以下步骤:

全选 User的:

Hibernate: 
select
user0_.id as id1_1_,
user0_.contact_details_id as contact_3_1_,
user0_.name as name2_1_
from
user user0_

现在急切地加载每个 UserContactDetailsUserDetails
Hibernate: 
select
contactdet0_.id as id1_0_0_,
contactdet0_.email as email2_0_0_,
contactdet0_.mobile as mobile3_0_0_
from
contact_details contactdet0_
where
contactdet0_.id=?
Hibernate:
select
userdetail0_.id as id1_2_2_,
userdetail0_.nationality as national2_2_2_,
userdetail0_.user_id as user_id3_2_2_,
user1_.id as id1_1_0_,
user1_.contact_details_id as contact_3_1_0_,
user1_.name as name2_1_0_,
contactdet2_.id as id1_0_1_,
contactdet2_.email as email2_0_1_,
contactdet2_.mobile as mobile3_0_1_
from
user_details userdetail0_
left outer join
user user1_
on userdetail0_.user_id=user1_.id
left outer join
contact_details contactdet2_
on user1_.contact_details_id=contactdet2_.id
where
userdetail0_.user_id=?

...

这通常称为 n + 1问题。见 here例如。

关于hibernate - 了解 Hibernate/JPA 对 findAll 和 findOne 的不同行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29901767/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com