title: Too many queries problem with JPA + Hibernate even when using @Fetch(FetchMode.JOIN)
body: <p>I am developing REST application using spring boot and I am trying to optimize the performance of the queries. I am currently using <code>findAll</code> from the repositories which is causing performance issues. Code is given below:</p>
  
  <p>Person Entity</p>
  
  <pre><code>@Entity
  @Table(name = "cd_person")
  @Data
  @NoArgsConstructor
  public class Person {
      ....
      @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
      @JoinColumn(name = "password_id")
      @Fetch(FetchMode.JOIN)
      private Password password;
      ....
      @ManyToMany(fetch = FetchType.EAGER, cascade = {CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH})
      @JoinTable(name = "cd_person_role",
          joinColumns = @JoinColumn(name = "person_id", referencedColumnName = "id"),
          inverseJoinColumns = @JoinColumn(name = "role_id", referencedColumnName = "id"))
      @Fetch(FetchMode.JOIN)
      private Set&lt;Role&gt; roles = new HashSet&lt;&gt;();
  }
  </code></pre>
  
  <p>Password Entity</p>
  
  <pre><code>@Entity
  @Table(name = "cd_password")
  @Data
  @NoArgsConstructor
  public class Password {
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      @Column(name = "id", updatable = false, nullable = false)
      private Long id;
  
      @Column(name = "password_hash", nullable = false)
      private String passwordHash;
      .......
  }
  </code></pre>
  
  <p>Role Entity</p>
  
  <pre><code>@Entity
  @Table(name = "cd_role")
  @Data
  @NoArgsConstructor
  public class Role {
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      private Long id;
  
      @Column(name = "role_type")
      @Enumerated(EnumType.STRING)
      private RoleType roleType;
      ....
  }
  </code></pre>
  
  <p>Person Repository</p>
  
  <pre><code>public interface PersonRepository extends CrudRepository&lt;Person, Long&gt; {
  
      Optional&lt;Person&gt; findByEmail(String email);
  
  }
  </code></pre>
  
  <p>When I do a <code>personRepository.findAll()</code> there are select queries fired for each row in the person table to fetch the password and roles when I access the person. I know I can use <code>@Query</code> annotation with <code>JOIN FETCH</code> in the repository to make it force generate the single query but I was wondering if there was any other way to do so. I am looking for something which we can do at the entity level to reduce queries. </p>
  
  <p>Using spring boot 2.1.5-RELEASE version and related dependencies. </p>
  
  <p>PS. The <code>@Data</code> and <code>@NoArgsConstructor</code> are Lombok annotations. </p>