博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Hibernate – Fetching Strategies Examples
阅读量:7005 次
发布时间:2019-06-27

本文共 4133 字,大约阅读时间需要 13 分钟。

hot3.png

Hibernate has few fetching strategies to optimize the Hibernate generated select statement, so that it can be as efficient as possible. The fetching strategy is declared in the mapping relationship to define how Hibernate fetch its related collections and entities.

Fetching Strategies

There are four fetching strategies

1. fetch-”join” = Disable the lazy loading, always load all the collections and entities.

2. fetch-”select” (default) = Lazy load all the collections and entities.
3. batch-size=”N” = Fetching up to ‘N’ collections or entities, *Not record*.
4. fetch-”subselect” = Group its collection into a sub select statement.

For detail explanation, you can check on the .

Fetching strategies examples

Here’s a “one-to-many relationship” example for the fetching strategies demonstration. A stock is belong to many stock daily records.

Example to declare fetch strategies in XML file

124636_LOPw_1417419.png

Example to declare fetch strategies in annotation

124804_SQw0_1417419.png

Let explore how fetch strategies affect the Hibernate generated SQL statement.

1. fetch=”select” or @Fetch(FetchMode.SELECT)

This is the default fetching strategy. it enabled the lazy loading of all it’s related collections. Let see the example…

124938_wyQm_1417419.png

Output

125041_r2il_1417419.png

Hibernate generated two select statements

1. Select statement to retrieve the Stock records -session.get(Stock.class, 114)

2. Select its related collections – sets.iterator()

2. fetch=”join” or @Fetch(FetchMode.JOIN)

The “join” fetching strategy will disabled the lazy loading of all it’s related collections. Let see the example…

125206_LfQz_1417419.png

Output

125240_Pcuc_1417419.png

Hibernate generated only one select statement, it retrieve all its related collections when the Stock is initialized. -session.get(Stock.class, 114)

1. Select statement to retrieve the Stock records and outer join its related collections.

3. batch-size=”10″ or @BatchSize(size = 10)

This ‘batch size’ fetching strategy is always misunderstanding by many Hibernate developers. Let see the *misunderstand* concept here…

125352_mgCa_1417419.png

What is your expected result, is this per-fetch 10 records from collection? See the output

Output

125432_k3M9_1417419.png

The batch-size did nothing here, it is not how batch-size work. See this statement.

The batch-size fetching strategy is not define how many records inside in the collections are loaded. Instead, it defines how many collections should be loaded.

— Repeat N times until you remember this statement —

Another example

Let see another example, you want to print out all the stock records and its related stock daily records (collections) one by one.

125550_kr9b_1417419.png

No batch-size fetching strategy

Output

125624_DiFL_1417419.png

If you have 20 stock records in the database, the Hibernate’s default fetching strategies will generate 20+1 select statements and hit the database.

1. Select statement to retrieve all the Stock records.

2. Select its related collection
3. Select its related collection
4. Select its related collection
….
21. Select its related collection

The generated queries are not efficient and caused a serious performance issue.

Enabled the batch-size=’10′ fetching strategy

Let see another example with batch-size=’10′ is enabled.

Output

125722_1LvG_1417419.png

Now, Hibernate will per-fetch the collections, with a select *in* statement. If you have 20 stock records, it will generate 3 select statements.

1. Select statement to retrieve all the Stock records.

2. Select In statement to per-fetch its related collections (10 collections a time)
3. Select In statement to per-fetch its related collections (next 10 collections a time)

With batch-size enabled, it simplify the select statements from 21 select statements to 3 select statements.

4. fetch=”subselect” or @Fetch(FetchMode.SUBSELECT)

This fetching strategy is enable all its related collection in a sub select statement. Let see the same query again..

125847_KYuQ_1417419.png

Output

125925_yAfk_1417419.png

With “subselect” enabled, it will create two select statements.

1. Select statement to retrieve all the Stock records.

2. Select all its related collections in a sub select query.

Conclusion

The fetching strategies are highly flexible and a very important tweak to optimize the Hibernate query, but if you used it in a wrong place, it will be a total disaster.

转载于:https://my.oschina.net/heroShane/blog/200015

你可能感兴趣的文章
我的友情链接
查看>>
中国何时亮剑 k8s搭建
查看>>
VMware 自定义规范管理器的配置
查看>>
如何查看水表
查看>>
我的友情链接
查看>>
oracle pk&fk
查看>>
20145328 《Java程序设计》第8周学习总结
查看>>
SIGABRT
查看>>
准爸爸日记——21120310
查看>>
二级缓存
查看>>
android实现卸载提示
查看>>
【译】ZFS最佳实践指南-Part6
查看>>
linux下挂载硬盘
查看>>
我的友情链接
查看>>
SWT关闭窗口弹出询问对话框 .
查看>>
Jenkins配置ldap错误导致无法登陆的问题
查看>>
shell awk sed tr grep 语法汇总
查看>>
让ubuntu10.04支持mysql的innodb引擎
查看>>
myeclipse 默认快捷键
查看>>
wiki
查看>>