title: List object mapped as EAGER is being fetched as LAZY - migrating Spring + Hibernate from 4 to 5 throwing org.hibernate.LazyInitializationException
body: <p>Spring version: 5.3.19
  Hibernate: 5.4.24.Final</p>
  <p>The problem: When trying to get the List compartments from class CriticalFlight after ScrollableData gets Cleaned up @Cleanup, the list is empty since fetch was never executed.</p>
  <p>Custom class ScrollableData execution snipped code:</p>
  <blockquote>
  <pre><code>List&lt;ENTITY&gt; filteredEntities;
  @Cleanup ScrollableData&lt;ENTITY&gt; scrollableData =
          getScrollableData(
              filter,
              myMarketChecker,
              additionalFilters,
              staticPredicateBuilders);
  
  filteredEntities = scrollableData.getAll();
  return filteredEntities;
  </code></pre>
  </blockquote>
  <p>I loop into the list returned and try to access List compartments
  then got: <code>&quot;org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.pros.travel.services.oandd.optimizer.alerts.entity.CriticalFlight.compartments, could not initialize proxy - no Session</code>
  &quot;</p>
  <p>Classes</p>
  <p>Embeddable Class: CriticalFlightKey</p>
  <blockquote>
  <pre><code>@Data
  @Embeddable
  public class CriticalFlightKey implements Serializable
  {
      @DTOMapping(CriticalFlightDTO.FIELD_FLIGHTDATE)
      @Convert(converter = DateToLocalDateAttributeConverter.class)
      @Column(name = &quot;FLTDATE&quot;, nullable = false)
      private LocalDate flightDate;
  
      @DTOMapping(CriticalFlightDTO.FIELD_DIM_CRRCODE)
      @Column(name = &quot;CRRCODE&quot;, nullable = false)
      private String carrierCode;
  
      @DTOMapping(CriticalFlightDTO.FIELD_DIM_FLTNUM)
      @Column(name = &quot;FLTNUM&quot;, nullable = false)
      private String flightNumber;
  
      @DTOMapping(CriticalFlightDTO.FIELD_DIM_ORGN)
      @Column(name = &quot;ORGN&quot;, nullable = false)
      private String origin;
  
      @DTOMapping(CriticalFlightDTO.FIELD_DIM_DSTN)
      @Column(name = &quot;DSTN&quot;, nullable = false)
      private String destination;
  }
  </code></pre>
  </blockquote>
  <p>Parent Class: CriticalFlight</p>
  <blockquote>
  <pre><code>@Data
  @EqualsAndHashCode(of = {&quot;id&quot;})
  @Entity
  @Table(name = &quot;OD_CRITICAL_FLIGHTS&quot;)
  public class CriticalFlight implements
  {
      @JsonUnwrapped
      @EmbeddedId
      @DTOMapped
      public CriticalFlightKey id;
  
      ...
      ....
  
  
  @JsonManagedReference
      @OneToMany(fetch = FetchType.EAGER)
      @Fetch(FetchMode.SUBSELECT)
      @JoinColumns({
          @JoinColumn(name=&quot;FLTDATE&quot;, referencedColumnName=&quot;FLTDATE&quot;),
          @JoinColumn(name=&quot;CRRCODE&quot;, referencedColumnName=&quot;CRRCODE&quot;),
          @JoinColumn(name=&quot;FLTNUM&quot;, referencedColumnName=&quot;FLTNUM&quot;),
          @JoinColumn(name=&quot;ORGN&quot;, referencedColumnName=&quot;ORGN&quot;),
          @JoinColumn(name=&quot;DSTN&quot;, referencedColumnName=&quot;DSTN&quot;)
      })
      private List&lt;CriticalFlightCmp&gt; compartments = new ArrayList&lt;&gt;();
  }
  </code></pre>
  </blockquote>
  <p>Embeddable class for child: CriticalFlightCmpKey</p>
  <blockquote>
  <pre><code>@Data
  @Embeddable
  public class CriticalFlightCmpKey implements Serializable
  {
      @Convert(converter = DateToLocalDateAttributeConverter.class)
      @Column(name = &quot;FLTDATE&quot;, nullable = false)
      private LocalDate flightDate;
  
      @Column(name = &quot;CRRCODE&quot;, nullable = false)
      private String carrierCode;
  
      @Column(name = &quot;FLTNUM&quot;, nullable = false)
      private String flightNumber;
  
      @Column(name = &quot;ORGN&quot;, nullable = false)
      private String origin;
  
      @Column(name = &quot;DSTN&quot;, nullable = false)
      private String destination;
  
      @Column(name = &quot;CMPCODE&quot;, nullable = false)
      private String cmpCode;
  }
  </code></pre>
  </blockquote>
  <p>Child Class: CriticalFlightCmp</p>
  <blockquote>
  <pre><code>@Data
  @EqualsAndHashCode(of = {&quot;id&quot;})
  @Entity
  @Table(name = &quot;OD_CRITICAL_FLIGHTS_CMP&quot;)
  public class CriticalFlightCmp implements IPersistable&lt;CriticalFlightCmpKey&gt;
  {
      @EmbeddedId
      private CriticalFlightCmpKey id;
  
      ..
      ...
  
  }
  </code></pre>
  </blockquote>
  <p>Custom class ScrollableData which uses org.hibernate.ScrollableResults to execute the query</p>
  <blockquote>
  <pre><code>@Slf4j
  public class ScrollableData&lt;ENTITY extends IPersistable&gt; implements Closeable
  {
      private static final int SCROLLABLE_FETCH_SIZE = 10000;
  
      private final Class&lt;ENTITY&gt; entityClass;
  
      private final ScrollableResults results;
  
      private final EntityManager entityManager;
  
      private final List&lt;IScrollableFilter&lt;ENTITY&gt;&gt; filters = new ArrayList&lt;&gt;();
  
      public ScrollableData(
          Class&lt;ENTITY&gt; entityClass,
          ScrollableResults results,
          EntityManager entityManager)
      {
          this.entityClass = entityClass;
          this.results = results;
          this.entityManager = entityManager;
      }
  
  
      /**
       * Create scrollable data from a query and entity manager session.
       *
       * @param entityManager Entity manager from which the query was built from.
       * @param query Query to scroll on.
       * @return Scrollable data
       */
      static &lt;ENTITY extends IPersistable&gt; ScrollableData&lt;ENTITY&gt; fromQuery(
          Class&lt;ENTITY&gt; entityClass,
          EntityManager entityManager,
          CriteriaQuery query)
      {
          ScrollableResults results = entityManager.createQuery(query)
              .unwrap(Query.class)
              .setReadOnly(true)
              .setFetchSize(SCROLLABLE_FETCH_SIZE)
              .setCacheable(false)
              .scroll(ScrollMode.FORWARD_ONLY);
  
          return new ScrollableData&lt;&gt;(entityClass, results, entityManager);
      }
  
  
      public List&lt;ENTITY&gt; getAll()
      {
          List&lt;ENTITY&gt; allEntities = new ArrayList&lt;&gt;();
  
          while (next())
          {
              allEntities.add(get());
          }
          
          return allEntities;
      }
  
      /**
       * Clears the hibernate session of any entities it's caching.
       */
      public void clearSession()
      {
          log.debug(&quot;Clearing Session for {}&quot;, entityClass.getSimpleName());
  
          Session hibernateSession = entityManager.unwrap(Session.class);
          hibernateSession.clear();
      }
  
      /**
       * Closes the scrollable results and the session contained in the entity manager.
       */
      public void close()
      {
          clearSession();
  
          if (results != null)
          {
              log.debug(&quot;Closing ScrollableResults for {}&quot;, 
  </code></pre>
  </blockquote>
  <p>entityClass.getSimpleName());</p>
  <blockquote>
  <pre><code>            results.close();
          }
  
          if (entityManager != null)
          {
              log.debug(&quot;Clearing EntityManager for {}&quot;, entityClass.getSimpleName());
              entityManager.close();
          }
      }
  
  }
  </code></pre>
  </blockquote>