title: Change fetch type in runtime of @ManyToMany Hibernate relationship
body: <p>I'm using Hibernate/JPA and QueryDSL. I have this tables in my database:</p>
  
  <p><strong>Proveedor</strong></p>
  
  <pre><code>IdProveedor | RazonSocial
  </code></pre>
  
  <p><strong>Empresa</strong></p>
  
  <pre><code>IdEmpresa | Nombre
  </code></pre>
  
  <p><strong>ProveedorEmpresa</strong></p>
  
  <pre><code>IdProveedor | IdEmpresa
  </code></pre>
  
  <p>There is a many-to-many relationship between <code>Proveedor</code> and <code>Empresa</code>. This is my JPA mapping:</p>
  
  <p><strong>PROVEEDOR</strong></p>
  
  <pre><code>@Entity
  public class Proveedor {
  
      private String idProveedor;
      private String razonSocial;
      @IgnoredField
      private List&lt;Empresa&gt; empresas;
  
      @Id
      @Column(name = "IdProveedor")
      public String getIdProveedor() {
          return idProveedor;
      }
  
      public void setIdProveedor(String idEmpresa) {
          this.idProveedor = idEmpresa;
      }
  
      @Basic
      @Column(name = "RazonSocial")
      public String getRazonSocial() {
          return razonSocial;
      }
  
      public void setRazonSocial(String razonSocial) {
          this.razonSocial = razonSocial;
      }
  
      @ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
      @JoinTable(name = "ProveedorEmpresa", joinColumns = {
              @JoinColumn(name = "IdProveedor", nullable = false, updatable = false)},
              inverseJoinColumns = {
                      @JoinColumn(name = "IdEmpresa", nullable = false, updatable = false)
              })
      public List&lt;Empresa&gt; getEmpresas() {
          return empresas;
      }
  
      public void setEmpresas(List&lt;Empresa&gt; empresas) {
          this.empresas = empresas;
      }
  
  }
  </code></pre>
  
  <p><strong>EMPRESA</strong></p>
  
  <pre><code>@Entity
  public class Empresa {
  
      private String idEmpresa;
      private String razonSocial;
      private String abreviatura;
      private List&lt;Proveedor&gt; proveedores;
  
      @Id
      @Column(name = "IdEmpresa")
      public String getIdEmpresa() {
          return idEmpresa;
      }
  
      public void setIdEmpresa(String idEmpresa) {
          this.idEmpresa = idEmpresa;
      }
  
      @Basic
      @Column(name = "RazonSocial")
      public String getRazonSocial() {
          return razonSocial;
      }
  
      public void setRazonSocial(String razonSocial) {
          this.razonSocial = razonSocial;
      }
  
      @Basic
      @Column(name = "Abreviatura")
      public String getAbreviatura() {
          return abreviatura;
      }
  
      public void setAbreviatura(String abreviatura) {
          this.abreviatura = abreviatura;
      }
  
      @ManyToMany(fetch = FetchType.EAGER, mappedBy = "empresas")
      public List&lt;Proveedor&gt; getProveedores() {
          return proveedores;
      }
  
      public void setProveedores(List&lt;Proveedor&gt; proveedores) {
          this.proveedores = proveedores;
      }
  }
  </code></pre>
  
  <p>I have this method that returns all the <code>Empresa</code> with their <code>Proveedor</code> (since is EAGER fetch):</p>
  
  <pre><code>public List&lt;Empresa&gt; getEmpresasWithProveedores() { 
          JPAQuery query = new JPAQuery();
          query.from(qEmpresa);
          return query.list(qEmpresa);
  }
  </code></pre>
  
  <p>Now, I want this other method that returns all the <code>Empresa</code> without their <code>Proveedor</code>:</p>
  
  <pre><code>public List&lt;Empresa&gt; getEmpresasWithoutProveedores() { 
          // Set the fetch type to LAZY
          JPAQuery query = new JPAQuery();
          query.from(qEmpresa);
          return query.list(qEmpresa);
  }
  </code></pre>
  
  <p>I have tried to add a <code>FetchProfile</code> in <code>Empresa</code> entity:</p>
  
  <pre><code>@Entity
  @FetchProfile(name = "WITH_PROVEEDORES", fetchOverrides = {
          @FetchProfile.FetchOverride(entity = Empresa.class, association = "proveedores", mode = FetchMode.JOIN)})
  public class Empresa { . . . }
  </code></pre>
  
  <p>and enable/disable it before each method. But it doesn't work, it has the same behaviour. Actually, I don't understand how <code>FetchProfile</code> works and how I have to configure it to obtain the behaviour that I want.</p>
  
  <p>Thanks in advance.</p>