Ltcs
Ltcs

Reputation: 283

FetchType.EAGER a lot of time of execution

In my entities I have to use fetch = FetchType.EAGER because I have a mistake:

nested exception is org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: "field should haver FetchType.EAGER", could not initialize proxy - no Session

If I use this then my app goes correct, but the time execution is too, between page and page around 7 seconds (now bbdd has little data)

I have two problems.

PF.class (Entity)

@OneToMany(fetch = FetchType.EAGER, mappedBy = "pf", cascade = CascadeType.ALL)
private Set<HDSEntity> hardwareDeviceStocks = new HashSet<HDSEntity>();

@OneToMany(fetch = FetchType.EAGER, mappedBy = "pf", cascade = CascadeType.ALL)
private Set<BSEntity> bS = new HashSet<BSEntity>();

@OneToMany(cascade = CascadeType.ALL, mappedBy = "pf", fetch = FetchType.EAGER)
private Set<CEntity> cp = new HashSet<CEntity>();

HDS.class (Entity)

 @ManyToOne(cascade = {CascadeType.ALL})
 @JoinColumn(name = "fk_pf")
 private PFEntity pf;

BS.class (Entity)

@ManyToOne(cascade = CascadeType.ALL)
 @JoinColumn(name = "fk_pf")
 private PFEntity pf;

Thanks. ;)

Upvotes: 0

Views: 1844

Answers (2)

N0nbot
N0nbot

Reputation: 190

Try reading this related question, which is almost the same issue you are facing:

Hibernate Criteria returns children multiple times with FetchType.EAGER

According to the answers given on the question above what is correct to do is keep using the EAGER fetch type to avoid the LazyInitiationException, however you must review your select queries and add some OUTER JOINs to reduce the query result.

It's really important to understand that this behavior on hibernate also occurs in native SQL queries, and that's because of it your pagination is so slow. Try to study some SQL OUTER JOINs too to minimize your pagination execution time.

Upvotes: 1

DmitryKanunnikoff
DmitryKanunnikoff

Reputation: 2266

Try to use FetchType.LAZY, but correct your queries with inner join fetch.

Upvotes: 0

Related Questions