title: JPA how to group by a collection property
body: <p>Is there a way to group by a collection property? For example,</p>
  
  <pre><code>public class Merchandise {
     id,
     name
  
  }
  
  public class Attribute {
      id,
      name,
      value,
  
      @ManyToOne
      MerchandiseCost merchandiseCost;
  }
  
  public class MerchandiseCost {
      Merchandise merchandise,
      List&lt;Attribute&gt; attributes,
      BigDecimal cost,
  }
  </code></pre>
  
  <p>Search MerchandiseCost group by merchandise and attributes.</p>
  
  <pre><code>select merchandise, attributes, sum(cost) from MerchandiseCost group by merchandise, attributes.
  </code></pre>
  
  <p>Will this be going to work? </p>
  
  <p>EDIT:
  If not, how to build a query to get results as following using CriteriaQuery API:</p>
  
  <pre><code>Merchandise         Attributes         SUM(COST)
  -----------------------------------------------------------
  Cloth          size:L, color:RED       10000
  Cloth          size:M, color:WHITE     20000
  Computer       Memory:4G               80000
  Computer       Memory:16G              90000
  </code></pre>