- In Jpa hibernate fetching entities with associtions has always been
a question for performance.
- Lazily loading associations with in a transaction again and again results in n+1 select issues and to avoid such issues JPQL
join fetch and Cr iteria api joins are used. But fetching data with
these two also result in Cross join issues means Cross join of all
table records are returned to apllication by hibernate.
- Also altering fetch variable defined in annotations on entity level is also not a good option for use case to use case basis .
- Hence to solve the above two issues entity graphs has been intoduced. All nodes defined in entity graphs are always eager
fetched irrespective of their definition on entity level. These
graphs are passed as a hint to queries .
- By passing graphs as a hint Cross join issues are solved as well annotation level specified fetch behaviour of an association can
also be changed .
For code you can check my github repository :
https://github.com/vaneetkataria/Jpa-Hibernate/blob/master/jdbcToJpaMigration/src/test/java/com/katariasoft/technologies/jpaHibernate/entity/fetch/entitygraph/dynamic/MultiInstructorsDynamicEntityGrpahTests.java