title: Spring - get a page of entity with lazy elements loaded
body: <p>I'm very new to Spring.</p>
  <p>I'm trying to get a Page to return that to my front end.
  The AppUser has a lazy association with Role entity.</p>
  <p>I'm getting a failed to lazily initialize a collection of role error.</p>
  <p>Did a lot of research but I do not get the issue.</p>
  <p>Let's see the code :</p>
  <p><strong>AppUser entity :</strong></p>
  <pre><code>@Entity
  @Data
  @Builder
  @AllArgsConstructor
  @NoArgsConstructor
  @Table(name = &quot;app_user&quot;)
  public class AppUser {
      @Id
      @GeneratedValue
      @Column(name = &quot;user_id&quot;)
      private UUID userId;
  
      @Column(name = &quot;user_first_name&quot;)
      private String userFirstName;
  
      @Column(name = &quot;user_last_name&quot;)
      @NonNull
      private String userLastName;
  
      @Column(name = &quot;matricule&quot;)
      private String matricule;
  
      @Column(name = &quot;email_address&quot;)
      private String emailAddress;
  
      @Column(name = &quot;password&quot;)
      private String password;
  
      @EqualsAndHashCode.Exclude
      @ManyToOne
      @JoinColumn(name = &quot;grade_id&quot;)
      private Grade grade;
  
      @EqualsAndHashCode.Exclude
      @ManyToMany
      @JoinTable(name = &quot;user_role_association&quot;, joinColumns = @JoinColumn(name = &quot;user_id&quot;), 
  inverseJoinColumns = @JoinColumn(name = &quot;application_role_id&quot;))
      private List&lt;AppRole&gt; appRoles;
  </code></pre>
  <p><strong>Controller method :</strong></p>
  <pre><code>    @GetMapping(path = &quot;user&quot;, produces = MediaType.APPLICATION_JSON_VALUE)
  public ResponseEntity&lt;Page&lt;AppUser&gt;&gt; getUserPage(@AuthenticationPrincipal UserDetail user,
                                                   @RequestParam(&quot;page&quot;) int page,
                                                   @RequestParam(&quot;size&quot;) int size,
                                                   @RequestParam(value = &quot;sortDirection&quot;, 
  required = false) String sortDirection,
                                                   @RequestParam(value = &quot;sortField&quot;, required = 
  false) String sortField) {
      log.info(String.format(&quot;User %s getting list of all users&quot;, user.getUsername()));
      try {
          final Page&lt;AppUser&gt; response = appUserService.getAll(page, size,sortDirection, 
  sortField);
          return ResponseEntity.ok(response);
      } catch (Exception e) {
          log.error(&quot;Error fetching user page&quot;, e);
          throw new ResponseStatusException(HttpStatus.BAD_REQUEST, &quot;Error fetching data&quot;);
      }
  }
  </code></pre>
  <p><strong>Service method</strong> :</p>
  <pre><code>    public Page&lt;AppUser&gt; getAll(int page,
                              int size,
                              String sortDirection,
                              String sortField) {
      Pageable pageable;
      Sort sort;
      // sorting &amp; pagination
      if (sortField != null &amp;&amp; !sortField.isEmpty()) {
          sort = Sort.by(sortField);
          if (sortDirection != null &amp;&amp; sortDirection.equals(&quot;desc&quot;)) {
              sort = sort.descending();
          }
      } else {
          sort = Sort.by(&quot;matricule&quot;).descending();
      }
      pageable = PageRequest.of(page, size, sort);
  
      return appUserRepository.findAllAppUserWithAllProperties(pageable);
  }
  </code></pre>
  <p><strong>Repository</strong></p>
  <pre><code>@Repository
  public interface AppUserRepository extends JpaRepository&lt;AppUser, UUID&gt; {
  Optional&lt;AppUser&gt; findByUserId(UUID uuid);
  AppUser findByMatricule(String matricule);
  
  @Query(value = &quot;SELECT u FROM AppUser u LEFT JOIN FETCH u.appRoles WHERE u.matricule = 
  :matricule&quot;)
  AppUser findAppUserWithAppRolesByMatricule(String matricule);
  
  @Query(value = &quot;SELECT u FROM AppUser u LEFT JOIN u.appRoles&quot;)
  Page&lt;AppUser&gt; findAllAppUserWithAllProperties(Pageable pageable);
  </code></pre>
  <p>}</p>
  <p>Looking at the logs I can see the query runs :</p>
  <pre><code>SQL - select appuser0_.user_id as user_id1_0_, appuser0_.email_address as email_ad2_0_, 
  appuser0_.grade_id as grade_id7_0_, appuser0_.matricule as matricul3_0_, appuser0_.password as 
  password4_0_, appuser0_.user_first_name as user_fir5_0_, appuser0_.user_last_name as 
  user_las6_0_ from rixheim.app_user appuser0_ left outer join rixheim.user_role_association 
  approles1_ on appuser0_.user_id=approles1_.user_id left outer join rixheim.application_role 
  approle2_ on approles1_.application_role_id=approle2_.application_role_id order by 
  appuser0_.matricule desc limit ?
  </code></pre>
  <p>So I guess I'm not so far of a way to retrieve the full object but I'm really stuck now.</p>
  <p>Appreciate if you can help on that matter.</p>
  <p>Thanks.</p>