Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between @LazyCollection(LazyCollectionOption.FALSE) and @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;

I have this error:

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

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;

it works perfectly.

sorry for my English thanks

like image 505
Gilvan André Avatar asked Sep 23 '14 13:09

Gilvan André


People also ask

What is fetch FetchType eager?

Fetch type Eager is essentially the opposite of Lazy, Eager will by default load ALL of the relationships related to a particular object loaded by Hibernate.

What is the difference between fetch type eager and lazy?

LAZY: It fetches the child entities lazily i.e at the time of fetching parent entity it just fetches proxy(created by cglib or any other utility) of the child entities and when you access any property of child entity then it is actually fetched by hibernate. EAGER: it fetches the child entities along with parent.

What is FetchType eager in JPA?

Enum FetchType. Defines strategies for fetching data from the database. The EAGER strategy is a requirement on the persistence provider runtime that data must be eagerly fetched. The LAZY strategy is a hint to the persistence provider runtime that data should be fetched lazily when it is first accessed.

What is FetchType eager in Hibernate?

EAGER fetching tells Hibernate to get the related entities with the initial query. This can be very efficient because all entities are fetched with only one query. But in most cases it just creates a huge overhead because you select entities you don't need in your use case. You can prevent this with FetchType.


2 Answers

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 on the other hand, is Hibernate specific, and obviously works only if Hibernate is used.

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).

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?

like image 68
Magnilex Avatar answered Sep 30 '22 16:09

Magnilex


As proposed in this post : https://stackoverflow.com/a/5865605/5619076 Changing List by Set should solve the MultipleBagFetchException. That solved mine.

like image 30
OlivierTerrien Avatar answered Sep 30 '22 17:09

OlivierTerrien