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

编程入门 行业动态 更新时间:2024-10-28 02:31:24
本文介绍了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 规范,但我仍然不确定如何正确使用这两个选项......

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

问题是……

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

我将首先回答您问题的第二部分.

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

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

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

在 JPA 中加载实体有两种主要方式,eager 加载和 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.persistence.fetchgraph 来指定 FETCH 作为您的策略.在这种情况下,实体图中指定的所有属性都将被视为 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:05:49,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1626458.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:有什么区别   实体   JPA   LOAD   FETCH

发布评论

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

>www.elefans.com

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