For further reference, please consider the following sections:
- Official Gradle documentation
- Spring Boot Gradle Plugin Reference Guide
- Create an OCI image
- Spring Web
- Spring Data JPA
The following guides illustrate how to use some features concretely:
- Building a RESTful Web Service
- Serving Web Content with Spring MVC
- Building REST services with Spring
- Accessing Data with JPA
- Accessing data with MySQL
These additional references should also help you:
We can use EntityGraph to formulate better-performing queries and deal with performance loading.
In this application Status, subStatuses and address are loaded lazily. The method findAll of UserRepository return UserEntity from user table data with single query. But when we are calling getStatus() of UserEntity it will call one more select query and again it will generate new query when we will call getSubStatuses() of StatusEntity, Similarly for getAddress it will generate +1 query.
Here is table and data.
findAll() method of UserRepository: Below is the Test Result screenshot, we can see there are 9 queries generated.
1st query to get user list.
3 status table query by 3 status Ids(1,2,3).
3 SubStatus table query by substatus ids(1,2,3) and
2 address table query by address ids(1,2).
So, If we have huge data then number of query will be increase.
API response time without EntityGraph: 26.0ms
This can be easily achieved by 1 query using join. We can use EntityGraph with different FetchType Strategies to achieve this.
Fetch(EntityGraph.EntityGraphType.FETCH): The attributes that are specified by attributeNodes are treated as FetchType.EAGER and the rest of the attributes are treated as FetchType.LAZY
Load(EntityGraph.EntityGraphType.LOAD): The attributes that are specified by attributeNodes are treated as FetchType.EAGER and the rest of the attributes are treated according to their specified or default FetchType
It defaults to EntityGraph.EntityGraphType.FETCH.
findAll() method of UserRepository:
Below is the Test Result screenshot with EntityGraph Here we are seeing there is 1 query generated using left outer join
API response time with EntityGraph: 14.3ms
-
You can use https://github.com/Cosium/spring-data-jpa-entity-graph
Add in build.gradle file: -
Extends EntityGraphJpaRepository and EntityGraphJpaSpecificationExecutor in repository interface