title: Hibernate querying
body: <p>I'm sorry for my maybe foolish question. I have products and orders tables (with many-to -many relationship), also i have an user table. And now I want to get the product count by user_id and by special field "order_satus". I can make two queries get order by special criteria and then get size of product in order. But this is not optimal at all. When i use JDBCTemplate I did a lot of joins and get only one query.</p>
  
  <p>Here are my entities:</p>
  
  <pre><code>@Entity
  @Table(name = "shop.order")
  public class Order {
  @Id
  @Column(name = "order_id")
  @GeneratedValue(strategy = GenerationType.AUTO)
  private long orderId;
  
  private long user_id;
  
  @Column(name = "databegin")
  private Date dateBegin;
  
  @Column(name = "dataend")
  private Date dateEnd;
  
  @ManyToOne(fetch = FetchType.LAZY)
  @JoinColumn(name = "user_id", insertable = false, updatable = false)
  private User user;
  
  @ManyToMany(fetch = FetchType.LAZY)
  @JoinTable(name = "order_product", joinColumns = { @JoinColumn(name = "order_id") }, inverseJoinColumns = { @JoinColumn(name = "product_id") })
  private List&lt;Product&gt; products;
  }
  </code></pre>
  
  <p>Product Entity</p>
  
  <pre><code> @Entity
      @Table(name = "product")
      public class Product {
  
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      private int product_id;
  
  
      @ManyToMany(fetch = FetchType.LAZY)
      @JoinTable(name = "order_product", joinColumns = { @JoinColumn(name = "product_id") }, inverseJoinColumns = { @JoinColumn(name = "order_id") })
      private List&lt;Order&gt; order;
  
      public List&lt;Order&gt; getOrder() {
          return order;
      }
  
      public void setOrder(List&lt;Order&gt; order) {
          this.order = order;
      }
  
      @Column
      @NotBlank
      private String name;
  
      @Column
      @Max(value = 250)
      private String descr;
  
      @Column
      @Max(value = 250)
      private String manufacturer;
  
      @Column
      private Double price;
  
      @Column
      private Byte[] barcode;
  
      @Column
      private Byte[] picture;
  
      @ForeignKey(name = "category_id")
      private int category_id;
  </code></pre>