title: Hibernate creating N+1 queries for @ManyToOne JPA annotated property
body: <p>I have these classes:</p>
  
  <pre><code>@Entity
  public class Invoice implements Serializable {
      @Id
      @Basic(optional = false)
      private Integer number;
  
      private BigDecimal value;
  
      //Getters and setters
  }
  
  @Entity
  public class InvoiceItem implements Serializable {
      @EmbeddedId
      protected InvoiceItemPK invoiceItemPk;
  
      @ManyToOne
      @JoinColumn(name = "invoice_number", insertable = false, updatable = false)
      private Invoice invoice;
  
      //Getters and setters
  }
  </code></pre>
  
  <p>When i run this query:</p>
  
  <pre><code>session.createQuery("select i from InvoiceItem i").list();
  </code></pre>
  
  <p>It executes one query to select the records from InvoiceItem, and if I have 10000 invoice items, it generates 10000 additional queries to select the Invoice from each InvoiceItem.</p>
  
  <p>I think it would be a lot better if all the records could be fetched in a single sql. Actually, I find it weird why it is not the default behavior.</p>
  
  <p>So, how can I do it?</p>