Sponsored Links
Ad by Google
Hibernate is very popular ORM framework, and it has various features. One of the important and popular feature is query optimization. To improve the performance of Hibernate query Hibernate provides four fetching strategies and those are listed below with example.
Fetching strategies supported by Hibernate:
Subselect fetching: a second SELECT is used to retrieve the associated collections for all entities retrieved in a previous query or fetch. Unless you explicitly disable lazy fetching by specifying lazy="false", this second select will only be executed when you access the association. Only two select query will be executed in Subselect fetching using sub select. You can see the complete example of Subselect fetching, here is the out put of the Subselect fetching.
Batch fetching: an optimization strategy for select fetching. Hibernate retrieves a batch of entity instances or collections in a single SELECT by specifying a list of primary or foreign keys. Here is complete example of Batch Fetching with detail implementation see the output.
Select fetching: a second SELECT is used to retrieve the associated entity or collection. Unless you explicitly disable lazy fetching by specifying lazy="false", this second select will only be executed when you access the association. This is the default fetching strategy supported by Hibernate.
References
Reference 1
Reference 2
Reference 3
Fetching strategies supported by Hibernate:
- Join fetching (@Fetch(FetchMode.JOIN))
- Subselect fetching (@Fetch(FetchMode.SUBSELECT))
- Batch fetching (@BatchSize(size=x))
- Select fetching (@Fetch(FetchMode.SELECT))
Hibernate:
select
author0_.author_id as author_i1_0_0_,
author0_.dob as dob2_0_0_,
author0_.first_name as first_na3_0_0_,
author0_.last_name as last_nam4_0_0_,
books1_.author_id as author_i4_0_1_,
books1_.book_id as book_id1_1_1_,
books1_.book_id as book_id1_1_2_,
books1_.author_id as author_i4_1_2_,
books1_.isbn as isbn2_1_2_,
books1_.title as title3_1_2_
from
author author0_
left outer join
book books1_
on author0_.author_id=books1_.author_id
where
author0_.author_id=?
Subselect fetching: a second SELECT is used to retrieve the associated collections for all entities retrieved in a previous query or fetch. Unless you explicitly disable lazy fetching by specifying lazy="false", this second select will only be executed when you access the association. Only two select query will be executed in Subselect fetching using sub select. You can see the complete example of Subselect fetching, here is the out put of the Subselect fetching.
Hibernate:
select
author0_.author_id as author_i1_0_,
author0_.dob as dob2_0_,
author0_.first_name as first_na3_0_,
author0_.last_name as last_nam4_0_
from
author author0_
Hibernate:
select
books0_.author_id as author_i4_0_1_,
books0_.book_id as book_id1_1_1_,
books0_.book_id as book_id1_1_0_,
books0_.author_id as author_i4_1_0_,
books0_.isbn as isbn2_1_0_,
books0_.title as title3_1_0_
from
book books0_
where
books0_.author_id in (
select
author0_.author_id
from
author author0_
)
Batch fetching: an optimization strategy for select fetching. Hibernate retrieves a batch of entity instances or collections in a single SELECT by specifying a list of primary or foreign keys. Here is complete example of Batch Fetching with detail implementation see the output.
Hibernate:
select
author0_.author_id as author_i1_0_,
author0_.dob as dob2_0_,
author0_.first_name as first_na3_0_,
author0_.last_name as last_nam4_0_
from
author author0_
Hibernate:
select
books0_.author_id as author_i4_0_1_,
books0_.book_id as book_id1_1_1_,
books0_.book_id as book_id1_1_0_,
books0_.author_id as author_i4_1_0_,
books0_.isbn as isbn2_1_0_,
books0_.title as title3_1_0_
from
book books0_
where
books0_.author_id in (
?, ?, ?, ?, ?
)
Select fetching: a second SELECT is used to retrieve the associated entity or collection. Unless you explicitly disable lazy fetching by specifying lazy="false", this second select will only be executed when you access the association. This is the default fetching strategy supported by Hibernate.
References
Reference 1
Reference 2
Reference 3
Sponsored Links





0 comments:
Post a Comment