Why we need to use entity graph?

前端 未结 1 1787
孤城傲影
孤城傲影 2021-01-21 07:50

I’ve been leaning about JPA and I found that we can use entity graph since JPA 2.1.

But I\'ve not understood the merit of entity graph yet.

相关标签:
1条回答
  • 2021-01-21 08:27
    1. 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

    0 讨论(0)
提交回复
热议问题