为什么我们需要使用实体图? [英] Why we need to use entity graph?

查看:139
本文介绍了为什么我们需要使用实体图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在考虑 JPA ,我发现从 JPA 2.1开始就可以使用实体图。 / p>

但是我还不了解实体图的优点。



我知道其中一项优点使用实体图是我们只能在整个实体中指定我们想要获取的数据,但是如果我们想要整个实体,还有其他理由使用实体图吗?
还是仅在我们要检索部分数据时才应使用实体图?



使用实体图时还有其他目的或优点,我想知道这一点。

解决方案


  1. 在Jpa休眠状态下,具有关联性的实体获取始终是
    a性能问题。


    • 在事务中一次又一次地缓慢加载关联会导致n + 1个选择问题并避免此类问题JPQL
      join fetch和Cr iteria使用api连接。但是,使用
      来获取这两个数据也会导致交叉连接问题,这意味着所有
      表记录的交叉连接都将由休眠返回到合并。

    • 对于基于用例而言,在实体级别上更改注释中定义的访存变量也不是一个好选择。

    • 因此引出了解决以上两个问题的实体图。实体图中定义的所有节点始终渴望获取
      ,无论其在实体级别上的定义如何。这些
      图形作为提示传递给查询。

    • 通过图形传递作为提示,可以解决交叉连接问题以及注释级别指定的关联访存行为可以
      也可以更改。


对于代码,您可以检查我的github存储库:



https://github.com/vaneetkataria/Jpa-Hibernate/blob/master/jdbcToJpaMigration/src/test/java/com/ katariasoft / technologies / jpaHibernate / entity / fetch / entitygraph / dynamic / MultiInstructorsDynamicEntityGrpahTests.java


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.

I know that one of the merit to use entity graph is we can specify only the data which we want to get among the whole entity, but in case we want to whole entity, is there any other reason to use entity graph? or we should use entity graph only when we want to retrieve partial data?

it there is other purpose or merit when we use entity graph, I would like to know it.

解决方案

  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

这篇关于为什么我们需要使用实体图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆