java @LazyCollection(LazyCollectionOption.FALSE) 和@OneToMany(fetch = FetchType.EAGER) 的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25996758/
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
Difference between @LazyCollection(LazyCollectionOption.FALSE) and @OneToMany(fetch = FetchType.EAGER)
提问by Gilvan André
I have one doubt about "Lazy-loading".
What's the difference between use of @LazyCollection(LazyCollectionOption.FALSE)
and @OneToMany(fetch = FetchType.EAGER)
?
我对“延迟加载”有一个疑问。什么是使用之间的区别 @LazyCollection(LazyCollectionOption.FALSE)
和@OneToMany(fetch = FetchType.EAGER)
?
In my application I use two lists, but if I use in this format:
在我的应用程序中,我使用了两个列表,但如果我使用这种格式:
@OneToMany(mappedBy = "consultaSQL", orphanRemoval = true, fetch = FetchType.EAGER,
cascade = CascadeType.ALL)
private List<ParametroSQL> parametros;
@OneToMany(mappedBy = "consulta", orphanRemoval = true, fetch = FetchType.EAGER,
cascade = CascadeType.ALL)
private List<Contato> contatos;
I have this error:
我有这个错误:
Caused by: org.hibernate.loader.MultipleBagFetchException: cannot simultaneously fetch multiple bags
引起:org.hibernate.loader.MultipleBagFetchException:无法同时获取多个包
I know this occurs because the Hibernate does not allow me to loading two lists at the same time. But if I use this format:
我知道这是因为 Hibernate 不允许我同时加载两个列表。但是如果我使用这种格式:
@LazyCollection(LazyCollectionOption.FALSE)
@OneToMany(mappedBy = "consultaSQL", orphanRemoval = true,
cascade = CascadeType.ALL)
private List<ParametroSQL> parametros;
@LazyCollection(LazyCollectionOption.FALSE)
@OneToMany(mappedBy = "consulta", orphanRemoval = true,
cascade = CascadeType.ALL)
private List<Contato> contatos;
it works perfectly.
它完美地工作。
sorry for my English thanks
对不起我的英语谢谢
采纳答案by Magnilex
The fundamental difference between the annotations is that @OneToMany
and its parameters (e.g. fetch = FetchType.EAGER
) is a pure JPA. It can be used with any JPA provider, such as Hibernate or EclipseLink.
注释之间的根本区别在于@OneToMany
它的参数(例如fetch = FetchType.EAGER
)是纯 JPA。它可以与任何 JPA 提供程序一起使用,例如 Hibernate 或 EclipseLink。
@LazyCollection
on the other hand, is Hibernate specific, and obviously works only if Hibernate is used.
@LazyCollection
另一方面,是特定于 Hibernate 的,显然只有在使用 Hibernate 时才有效。
If possible, try to stick to the JPA specification as much as possible. By doing this, you should be able to switch provider easily (at least in theory).
如果可能,尽量坚持 JPA 规范。通过这样做,您应该能够轻松切换提供程序(至少在理论上)。
As for your real problem, could it be that you are using a Hibernate version that doesn't support JPA 2.0 as thisanswer suggests?
至于您真正的问题,是否如此答案所暗示的那样,您使用的是不支持 JPA 2.0 的 Hibernate 版本?
回答by Gora
I think i got this same problem...
我想我遇到了同样的问题...
try on all collections...
试穿所有系列...
@Fetch(FetchMode.SUBSELECT)
It should eliminate error
它应该消除错误
回答by OlivierTerrien
As proposed in this post : https://stackoverflow.com/a/5865605/5619076Changing List by Set should solve the MultipleBagFetchException. That solved mine.
正如这篇文章中所提出的:https: //stackoverflow.com/a/5865605/5619076按 Set 更改列表应该可以解决 MultipleBagFetchException。那解决了我的。