title: Hibernate generates many select statements for JPQL
body: <p>I wrote an JPQL to fetch a list of <code>ProductOrderU</code> entities for the given customer id, but I noticed that many select statements are generated, whereas I would expect only one. For example (I use Oracle):</p>
  <pre class="lang-sql prettyprint-override"><code>select * from product_order where order_id in (select order_id from orders where customer_id in (1));
  </code></pre>
  <p>Hibernate generates:</p>
  <pre><code>Hibernate: select productord0_.order_id as order_id1_8_, productord0_.product_id as product_id2_8_, productord0_.amount_of_ordered_products as amount_of_ordered_3_8_ from product_order productord0_ cross join orders orderu1_ where productord0_.order_id=orderu1_.order_id and orderu1_.customer_id=?
  Hibernate: select orderu0_.order_id as order_id1_6_0_, orderu0_.cost_of_delivery as cost_of_delivery2_6_0_, orderu0_.cost_of_products as cost_of_products3_6_0_, orderu0_.customer_id as customer_id5_6_0_, orderu0_.delivery_id as delivery_id6_6_0_, orderu0_.final_cost as final_cost4_6_0_ from orders orderu0_ where orderu0_.order_id=?
  Hibernate: select productu0_.product_id as product_id1_7_0_, productu0_.amount_in_stock as amount_in_stock2_7_0_, productu0_.category_id as category_id11_7_0_, productu0_.description as description3_7_0_, productu0_.product_name as product_name4_7_0_, productu0_.product_price as product_price5_7_0_, productu0_.production_year as production_year6_7_0_, productu0_.promotion_id as promotion_id12_7_0_, productu0_.&quot;size&quot; as size7_7_0_, productu0_.sold_amount as sold_amount8_7_0_, productu0_.vendor as vendor9_7_0_, productu0_.weight as weight10_7_0_ from product productu0_ where productu0_.product_id=?
  Hibernate: select complaints0_.order_id as order_id1_2_0_, complaints0_.complaint_id as complaint_id2_2_0_, complaintu1_.complaint_id as complaint_id1_1_1_, complaintu1_.complaint_time as complaint_time2_1_1_, complaintu1_.content as content3_1_1_, complaintu1_.handling_time as handling_time4_1_1_ from complaints_for_orders complaints0_ inner join complaint complaintu1_ on complaints0_.complaint_id=complaintu1_.complaint_id where complaints0_.order_id=?
  Hibernate: select customeru0_.customer_id as customer_id1_3_0_, customeru0_.city as city2_3_0_, customeru0_.email as email3_3_0_, customeru0_.first_name as first_name4_3_0_, customeru0_.flat_number as flat_number5_3_0_, customeru0_.house_number as house_number6_3_0_, customeru0_.password as password7_3_0_, customeru0_.phone_number as phone_number8_3_0_, customeru0_.postal_code as postal_code9_3_0_, customeru0_.street as street10_3_0_, customeru0_.surname as surname11_3_0_ from customer customeru0_ where customeru0_.customer_id=?
  Hibernate: select deliveryu0_.delivery_id as delivery_id1_4_0_, deliveryu0_.delivery_company_id as delivery_company_i4_4_0_, deliveryu0_.delivery_time as delivery_time2_4_0_, deliveryu0_.price as price3_4_0_, deliveryu0_.promotion_id as promotion_id5_4_0_ from delivery deliveryu0_ where deliveryu0_.delivery_id=?
  Hibernate: select deliveryco0_.delivery_company_id as delivery_company_i1_5_0_, deliveryco0_.delivery_company_name as delivery_company_n2_5_0_ from delivery_company deliveryco0_ where deliveryco0_.delivery_company_id=?
  Hibernate: select categoryu0_.category_id as category_id1_0_0_, categoryu0_.category_name as category_name2_0_0_, categoryu0_.parent_category_id as parent_category_id3_0_0_, categoryu0_.promotion_id as promotion_id4_0_0_ from category categoryu0_ where categoryu0_.category_id=?
  Hibernate: select categoryu0_.category_id as category_id1_0_0_, categoryu0_.category_name as category_name2_0_0_, categoryu0_.parent_category_id as parent_category_id3_0_0_, categoryu0_.promotion_id as promotion_id4_0_0_ from category categoryu0_ where categoryu0_.category_id=?
  
  </code></pre>
  <p>The question is why? Something is bad with the query or so many select statements just have to generated.</p>
  <p>Method for fetching:</p>
  <pre class="lang-java prettyprint-override"><code>@Transactional
  public List&lt;ProductOrderU&gt; fetchProductOrders(Long customerId)
  {
     return entityManager.createQuery(&quot;select po from ProductOrderU po where po.order.customer.customerId = :customerId&quot;, ProductOrderU.class)
                  .setParameter(&quot;customerId&quot;, customerId).getResultList();
  }
  </code></pre>
  <p><code>ProductOrderU</code> is a parent entity for several others:</p>
  <pre class="lang-java prettyprint-override"><code>@Entity
  @Table(name = &quot;product_order&quot;)
  @Setter
  @Getter
  public class ProductOrderU
  {
      @EmbeddedId
      private ProductOrderIdU productOrderId = new ProductOrderIdU();
  
      private Long amountOfOrderedProducts;
  
      @ManyToOne(optional = false, fetch = FetchType.LAZY)
      @MapsId(&quot;productId&quot;)
      @JoinColumn(name = &quot;product_id&quot;, nullable = false)
      private ProductU product;
  
      @ManyToOne(optional = false, fetch = FetchType.LAZY)
      @MapsId(&quot;orderId&quot;)
      @JoinColumn(name = &quot;order_id&quot;, nullable = false)
      private OrderU order;
  }
  </code></pre>
  <p>And the relationships:
  <img src="https://i.stack.imgur.com/OnK3z.png" alt="Relationship" /></p>