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
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.
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.
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.
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.
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?
As proposed in this post : https://stackoverflow.com/a/5865605/5619076 Changing List by Set should solve the MultipleBagFetchException. That solved mine.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With