对于JPA的实体图,FETCH和LOAD有什么区别?

编程入门 行业动态 更新时间:2024-10-28 10:34:06
本文介绍了对于JPA的实体图,FETCH和LOAD有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我是JPA的新手,我正在尝试使用实体图。 我意识到当我检索数据时,我需要指定我想要使用的EntityGraphType。

I'm new to JPA and I'm trying to use entity graph. And I realized when I retrieve data, I need to specify which EntityGraphType I want to use.

我读过JPA2.1规范但是我还没有我怎么能正确使用这2个选项......

I read JPA2.1 specification but I'm still not sure how can I use these 2 options properly...

问题是......

  • 如果我没有任何具体要求,我应该使用哪个选项?
  • 当我需要使用Fetch and Load时,具体情况是什么?
推荐答案

我将首先回答你问题的第二部分。

I will begin by answering the second part of your question.

我需要使用Fetch and Load时的具体情况是什么?

what is the specific situation when I need to use Fetch and Load?

那里是在JPA中加载实体的两种主要方法,急切加载和 lazy 加载。在急切加载时,实体在其父加载时立即加载。在延迟加载中,仅在调用该实体的实际getter时加载实体。高性能应用程序倾向于偏向于延迟加载,因为在应用程序启动时使最终用户等待整个表甚至组表加载并不是很好。现在转到第二个问题。

There are two primary ways to load an entity in JPA, eager loading and lazy loading. In eager loading, an entity is immediately loaded at the time its parent gets loaded. In lazy loading, an entity is only loaded when an actual getter for that entity is called. High performance applications tend to be biased towards lazy loading because it isn't very nice to make the end user wait for an entire table, or even group of tables, to load when the application starts up. Now on to your second question.

通过导入 javax指定 FETCH 作为策略包含实体的文件中的.persistence.fetchgraph 。在这种情况下,实体图中指定的所有属性都将被视为 FetchType.EAGER ,并且所有未指定的属性将被视为 FetchType.LAZY 。另一方面,如果通过导入 javax.persistence.loadgraph 指定 LOAD 作为策略,则指定所有属性实体图也是 FetchType.EAGER 但未指定的属性使用其指定的类型,如果实体未指定任何内容,则默认使用默认属性。

You specify FETCH as your strategy by importing javax.persistence.fetchgraph in the file containing the entity. In this case, all attributes specified in your entity graph will be treated as FetchType.EAGER, and all attributes not specified will be treated as FetchType.LAZY. On the other hand, if you specify LOAD as your strategy by importing javax.persistence.loadgraph then all attributes specified in the entity graph are also FetchType.EAGER but attributes not specified use their specified type or default if the entity specified nothing.

如果我没有任何具体要求,我应该使用哪个选项?

which option should I use if I don't have any specific reqirement?

这就是说,你不太可能没有特定的要求。至少,您需要Web应用程序快速运行。因此,您可能应该默认为延迟加载。使用 FETCH 图表是不错的选择,因为在您认为应该急切加载属性的少数特殊情况下,它默认为延迟加载,但除外。

That being said, it is unlikely that you do not have a specific requirement. At the very least, you need your web application to run fast. For this reason, you should probably default to lazy loading. Using a FETCH graph is good option because it defaults to lazy loading except in the few special cases where you deem an attribute should be eagerly loaded.

这是一个链接到一个很棒的博客详细解释了所有这些,以及代码示例。

Here is a link to a great blog which explains all of this in detail, along with code samples.

更多推荐

对于JPA的实体图,FETCH和LOAD有什么区别?

本文发布于:2023-11-24 19:06:42,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1626462.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:有什么区别   实体   JPA   LOAD   FETCH

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!