Current tutorial we will get to know what is fetchMode, how to it is used to fetch the entity, introduction to @org.hibernate.annotations.Fetch annotation.
Setting up the Example
As an example we will use Customer entity with two properties, id and set of orders.
Next we will create Orders entity, consisting of Customer entity as a reference.
Next using test class, we will understand the usage of @Fetch annotation.
From fetching the customer details from database, we are using Hibernate session.
Below is the code to get customer details along with orders from database.
But there can be different ways to fetch the orders from database, either we can join with Customer entity to get the details, or generate single query for we can use another query to get all the order details.
1) FetchMode.SELECT: it is the default strategy, it will be used for fetching dependent lazily.
Fetch is used describe how hibernate should retrieve the dependent property when we lookup for a parent entity.
Using SELECT Hibernate will fetch orders entity lazily.
For suppose we want to fetch all the customers and their orders the code will look like below:
This will result the below generated SQL statements.
For each customer it will fetch the orders using individual query, this will result into very well-known n+1 select problem. First query is used to fetch the customers, and n queries are used for fetching each customer.
2) @BatchSize: Fetch mode as an optional configuration to specify the batch size, instead of retrieving the each customer order separately, Hibernate will fetch orders using batches. Using @BatchSize we can configure the batch size. This way we can decrease the number of queries required to fetch the orders.
3) FetchMode.JOIN: It is almost same eager loading, the orders and customer entities are queries using same query.
In this case for all required entities only single query is executed.
Hibernate generated SQL statement will look like this:
4) FetchMode.SUBSELECT: it is used only if the nested property is type of collection.
Using this fetchmode, only 2 queries are executed, one for fetching the customers, and one more query for fetching all customer orders.
And the Hibernate Generated queries look like below:
FetchMode vs. FetchType
In general, FetchMode defines how Hibernate will fetch the data (by select, join or subselect). FetchType, on the other hand, defines whether Hibernate will load data eagerly or lazily.
The exact rules between these two are as follows:
- 1) if the code doesn't set FetchMode, the default one is JOIN and FetchType works as defined
- 2) with FetchMode.SELECT or FetchMode.SUBSELECT set, FetchType also works as defined
- 3) with FetchMode.JOIN set, FetchType is ignored and a query is always eager
Necessary for me to keep in mind to connect FetchType when Java developer should be able to solve simple problems.
In this tutorial we learned about what FetchMode is, how Hibernate is using internally to fetch entity mappings, and different types of FetchModes and their usage, and finally we understood the differences between Fetch Mode and FetchType.
Source code: Hibenet Example Code