Skip to content
Advertisement

How does JPA EntityGraph allow chosing the entity graph at runtime?

I have seen various post describing that JPA EntityGraph allows to choose the graph at run time. And I am not entirely clear what this refers to.

Out of good faith and respect I would like to take this helpful article for reference: https://www.baeldung.com/jpa-entity-graph. (Most of JPA users might have gone through it already.)

The article quotes –

EntityGraph allows grouping the related persistence fields which we want to retrieve and lets us choose the graph type at runtime.

and again solidifies above statement in conclusion section.

In this article, we’ve explored using the JPA Entity Graph to dynamically fetch an Entity and its associations.

The decision is made at runtime in which we choose to load or not the related association.

As we see in the article (5.1) – EntityGraphs are defined as below using Annotations-
5.1. Defining an Entity Graph with Annotations

JavaScript

The @NameEntityGraph annotation is defined at compile time and I don’t see anything runtime or dynamic here.

But in 5.2 – entity graphs are defined using api or programmatically –
5.2. Defining an Entity Graph with the JPA API

JavaScript

In 5.2 approach, I see nodes can be chosen dynamically using some logic. So is this approach is what is refered to “dynamically fetch” and “runtime based”. Or am i missing something and do i have more to understand.

Further the approaches given in 6. Using the Entity Graph
ex:

JavaScript

are all programmatic and so can be changed during runtime i.e they can be said as dynamic.
But one approach missed in above article, but mentioned here – https://www.baeldung.com/spring-data-jpa-named-entity-graphs, as below, does not seem to fit in to dynamic criteria.

JavaScript

So does the dynamic approach just refer to 5.2 style or it implies even 5.1 style too.

Advertisement

Answer

You can’t use dynamic entity graphs with spring-data, because JpaRepository doesn’t have methods to pass entity graphs like

JavaScript

Using custom JPA repository

You can use raw JPA for that, by creating a custom repository and using entity graphs with EntityManager.

Using spring-data-jpa-entity-graph

There is a more convenient approach by using library spring-data-jpa-entity-graph.

It allows to use JPA repository methods like findById() or findByName() with dynamic entity graphs.

I prefer to use it with this helper class

JavaScript

Each entity has its own GraphBuilder

JavaScript

Repository uses EntityGraphJpaRepository from spring-data-jpa-entity-graph library

JavaScript

You can use derived query methods like findByName() with dynamic entity graphs too.

Example of using findById() method, the same approach can be applied to findByName()

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement