Code Monkey home page Code Monkey logo

spring-boot-jpa-entity-graph's Introduction

Getting Started

Reference Documentation

For further reference, please consider the following sections:

Guides

The following guides illustrate how to use some features concretely:

Additional Links

These additional references should also help you:

Overview

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.

User Table:

image

Status Table:

image

SubStatus Table:

image

Address Table:

image

Without EntityGraph:

findAll() method of UserRepository: image Below is the Test Result screenshot, we can see there are 9 queries generated. image

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 image

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.

With EntityGraph:

findAll() method of UserRepository: image

Below is the Test Result screenshot with EntityGraph image Here we are seeing there is 1 query generated using left outer join

API response time with EntityGraph: 14.3ms image

How to provide graph name dynamically?

  1. You can use https://github.com/Cosium/spring-data-jpa-entity-graph
    Add in build.gradle file: image

  2. Add EnableJpaRepositories in configuration or main class image

  3. Extends EntityGraphJpaRepository and EntityGraphJpaSpecificationExecutor in repository interface image

  4. Provide different graph name from service class image

spring-boot-jpa-entity-graph's People

Contributors

sovanmukherjee avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.