java JPA实体图的FETCH和LOAD有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31978011/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the difference between FETCH and LOAD for Entity graph of JPA?
提问by Naga
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.
我是 JPA 的新手,我正在尝试使用实体图。我意识到在检索数据时,我需要指定要使用的 EntityGraphType。
I read JPA2.1 specification but I'm still not sure how can I use these 2 options properly...
我阅读了 JPA2.1 规范,但我仍然不确定如何正确使用这两个选项......
the question is...
问题是...
- which option should I use if I don't have any specific reqirement?
- what is the specific situation when I need to use Fetch and Load?
- 如果我没有任何特定要求,我应该使用哪个选项?
- 当我需要使用 Fetch 和 Load 时,具体情况是什么?
回答by Tim Biegeleisen
I will begin by answering the second part of your question.
我将首先回答你问题的第二部分。
what is the specific situation when I need to use Fetch and Load?
当我需要使用 Fetch 和 Load 时,具体情况是什么?
There are two primary ways to load an entity in JPA, eagerloading and lazyloading. 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.
在 JPA 中加载实体有两种主要方式,即刻加载和延迟加载。在预先加载中,实体在其父级加载时立即加载。在延迟加载中,只有在调用实体的实际 getter 时才加载实体。高性能应用程序往往偏向于延迟加载,因为在应用程序启动时让最终用户等待整个表甚至一组表加载并不是很好。现在回答你的第二个问题。
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 notspecified 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.
您FETCH
可以通过导入javax.persistence.fetchgraph
包含实体的文件来指定您的策略。在这种情况下,实体图中指定的所有属性都将被视为FetchType.EAGER
,所有未指定的属性都将被视为FetchType.LAZY
。另一方面,如果您LOAD
通过导入指定策略,javax.persistence.loadgraph
则实体图中指定的所有属性也是FetchType.EAGER
但未指定的属性使用其指定类型或默认值(如果实体未指定任何内容)。
which option should I use if I don't have any specific reqirement?
如果我没有任何特定要求,我应该使用哪个选项?
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 exceptin the few special cases where you deem an attribute should be eagerly loaded.
话虽如此,您不太可能没有特定要求。至少,您需要让您的 Web 应用程序快速运行。出于这个原因,您可能应该默认为延迟加载。使用FETCH
图形是一个不错的选择,因为它默认为延迟加载,除非在您认为应该急切加载属性的少数特殊情况下。
Here is a link to a great blogwhich explains all of this in detail, along with code samples.