@LazyCollection(LazyCollectionOption.FALSE)和@OneToMany(fetch = FetchType.EAGER)之间的区别 [英] Difference between @LazyCollection(LazyCollectionOption.FALSE) and @OneToMany(fetch = FetchType.EAGER)

查看:255
本文介绍了@LazyCollection(LazyCollectionOption.FALSE)和@OneToMany(fetch = FetchType.EAGER)之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对延迟加载"有一个疑问. 使用@LazyCollection(LazyCollectionOption.FALSE)@OneToMany(fetch = FetchType.EAGER)有什么区别?

I have one doubt about "Lazy-loading". What's the difference between use of @LazyCollection(LazyCollectionOption.FALSE) and @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;

我有此错误:

原因:org.hibernate.loader.MultipleBagFetchException:无法同时获取多个包

Caused by: org.hibernate.loader.MultipleBagFetchException: cannot simultaneously fetch multiple bags

我知道发生这种情况是因为Hibernate不允许我同时加载两个列表.但是,如果我使用这种格式:

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:

@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;

它运行完美.

对不起,我的英语 谢谢

sorry for my English thanks

推荐答案

注释之间的根本区别在于@OneToMany及其参数(例如fetch = FetchType.EAGER)是纯JPA.它可以与任何JPA提供程序一起使用,例如Hibernate或EclipseLink.

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.

@LazyCollection是特定于Hibernate的,并且显然仅在使用Hibernate时有效.

@LazyCollection on the other hand, is Hibernate specific, and obviously works only if Hibernate is used.

如果可能,请尽量遵循JPA规范.这样,您应该能够轻松地切换提供程序(至少在理论上如此).

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 2.0的Hibernate版本,如

As for your real problem, could it be that you are using a Hibernate version that doesn't support JPA 2.0 as this answer suggests?

这篇关于@LazyCollection(LazyCollectionOption.FALSE)和@OneToMany(fetch = FetchType.EAGER)之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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