30 Aug

Spring Data JPA Entity Graphs

One of the important features in JPA 2.1 is the ability to specify fetch plans using Entity Graphs. This is useful since it allows you to customize the data that is retrieved with a query or find operation. It is expected to display data from the same entity in different and several ways when working with mid to large size applications. In other cases, you just need to select a smallest set of information to optimize the performance of your application.

Typically you do not have many mechanisms to control what is loaded or what is not loaded in a JPA Entity. You could use EAGER/LAZY fetching, but these definitions are pretty much static. You are unable to change their behaviour at runtime when retrieving data, meaning that you are stuck with what was defined in the entity. Changing these amid development is a nightmare, since it can cause queries to behave unexpectedly. Another way to control loading is to write specific JPQL queries.

When an object is retrieved from the datastore by JPA typically not all fields are retrieved immediately. This is because for efficiency purposes only particular field types are retrieved in the initial access of the object, and then any other objects are retrieved when required (lazy loading). The group of fields that are loaded is called an entity graph. There are three types of “entity graphs”

Default Entity Graph: implicitly defined in all JPA specs, specifying the fetch setting for each field/property (LAZY/EAGER).
Named Entity Graphs: a new feature in JPA 2.1 allowing the user to define Named Entity Graphs in metadata, via annotations or XML
Unnamed Entity Graphs: a new feature in JPA 2.1 allowing the user to define Entity Graphs via the JPA API at runtime

For more information on above three types of Entity Graphs you can refer to http://www.datanucleus.org/products/datanucleus/jpa/entity_graphs.html

Example

Step 1. Create below tables in MySQL database

Now we will create Spring Data JPA project in Eclipse.

Step 2. Create below pom.xml file

Step 3. Put the below logback.xml file under src/main/resources folder

Step 4. Create below applicatio.properties file under src/main/resources folder

Step 5. Create below model classes

Here in the above Department model class, we have NamedEntityGraph to include employeeCollection field loaded when Department object is loaded.

Step 5. Create below JPA Repositories in order to perform database activities

Here in the above DepartmentRepository, we want to fetch the department by department id and also we want to fetch all employees for this department using Entity Graph.

Step 6. Create below configuration class with MySQL datasource, transaction manager and entity manager

Step 7. Create below service classes

Step 8. Create below application configuration class

Step 9. Now create the main class which will test our application code

Step 10. Run the above main class you will see the below output with other logs in the console as well as in the log file created at the root level

Thanks for reading.

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.

Share