0

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.

  • If I put FetchType.LAZY my app does not work
  • If I put FetchType.EAGER my app has a lot of time of execution

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

Alexander
  • 3,129
  • 2
  • 19
  • 33
Ltcs
  • 263
  • 1
  • 3
  • 15

2 Answers2

1

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.

Community
  • 1
  • 1
N0nbot
  • 190
  • 1
  • thanks for your answer. I have been doing that you tell me, I have created a query into my repository to recover all pf with "LEFT OUTER JOIN": `@Query("SELECT p FROM PFEntity p LEFT OUTER JOIN p.hardwareDeviceStocks LEFT OUTER JOIN p.bS")` and I have removed "FetchType.EAGER" of my entities but this does not work. I continue with the same mistake "could not initialize proxy - no Session". I have been reading about you tell it to me and I have understood it well – Ltcs Mar 10 '14 at 10:01
  • That's not a mistake to use the "FetchType.EAGER" annotation, you just have to make sure that your query return the less amount of correct results as possible, so you will be able to reduce your pagination time. However, thanks for your return and good luck with your studies :) – N0nbot Mar 10 '14 at 11:32
0

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

DmitryKanunnikoff
  • 2,226
  • 2
  • 22
  • 35
  • This won't work - LazyInitializationException will be also thrown, because session is closed and someone calls getter on detached entity's collection. – Arek Woźniak Mar 07 '14 at 14:09