title: get value from count() in Criteria Api
body: <p>Can anyone help me to find a solution.
  I want to check if count method equals to 0 or not.</p>
  <pre><code>private Expression&lt;Long&gt; countProductReviewRatings(CriteriaBuilder cb, Join&lt;Product, ProductReview&gt; joinProductReview) {
      return cb.count(toBigDecimalProductReviewRating(cb, joinProductReview));
  }
  </code></pre>
  <p>this is the example of code I want to run</p>
  <p>here is method it uses</p>
  <pre><code>private Expression&lt;BigDecimal&gt; toBigDecimalProductReviewRating(CriteriaBuilder cb, Join&lt;Product, ProductReview&gt; joinProductReview) {
      return cb.selectCase()
              .when(cb.equal(joinProductReview.get(ProductReview_.RATING), Rating.FIVE), Rating.FIVE.getNumericValue())
              .when(cb.equal(joinProductReview.get(ProductReview_.RATING), Rating.FOUR_AND_HALF), Rating.FOUR_AND_HALF.getNumericValue())
              .when(cb.equal(joinProductReview.get(ProductReview_.RATING), Rating.FOUR), Rating.FOUR.getNumericValue())
              .when(cb.equal(joinProductReview.get(ProductReview_.RATING), Rating.THREE_AND_HALF), Rating.THREE_AND_HALF.getNumericValue())
              .when(cb.equal(joinProductReview.get(ProductReview_.RATING), Rating.THREE), Rating.THREE.getNumericValue())
              .when(cb.equal(joinProductReview.get(ProductReview_.RATING), Rating.TWO_AND_HALF), Rating.TWO_AND_HALF.getNumericValue())
              .when(cb.equal(joinProductReview.get(ProductReview_.RATING), Rating.TWO), Rating.TWO.getNumericValue())
              .when(cb.equal(joinProductReview.get(ProductReview_.RATING), Rating.ONE_AND_HALF), Rating.ONE_AND_HALF.getNumericValue())
              .when(cb.equal(joinProductReview.get(ProductReview_.RATING), Rating.ONE), Rating.ONE.getNumericValue())
              .otherwise(0)
              .as(BigDecimal.class);
  </code></pre>
  <p>Here is another method</p>
  <pre><code>    private Expression&lt;Long&gt; countProductDefaultRatings(CriteriaBuilder cb, Join&lt;Product, ProductDefaultRating&gt; joinProductDefaultRating) {
      return cb.count(joinProductDefaultRating.get(ProductDefaultRating_.DEFAULT_AVG_RATING));
  }
  </code></pre>
  <p>And here I run this methods</p>
  <pre><code>    private Expression&lt;Double&gt; calculateAverageRating(CriteriaBuilder cb, Join&lt;Product, ProductReview&gt; joinProductReview,
                                                    Join&lt;Product, ProductDefaultRating&gt; joinProductDefaultRating) {
  
      var countProductReviewRatings = countProductReviewRatings(cb, joinProductReview);
      var countProductDefaultRatings = countProductDefaultRatings(cb, joinProductDefaultRating);
  
      if (countProductReviewRatings.isNotNull().isNegated() &amp;&amp;
              countProductDefaultRatings.isNull().isNegated()) {
          return cb.quot(cb.sum(toBigDecimalProductReviewRating(cb, joinProductReview)), countProductReviewRatings).as(Double.class);
      }
      if (countProductReviewRatings.isNull().isNegated() &amp;&amp;
              countProductDefaultRatings.isNotNull().isNegated()) {
          return cb.quot(cb.sum(joinProductDefaultRating.get(ProductDefaultRating_.DEFAULT_AVG_RATING)), countProductDefaultRatings).as(Double.class);
      }
      if (countProductReviewRatings.isNotNull().isNegated() &amp;&amp;
              countProductDefaultRatings.isNotNull().isNegated()) {
          return cb.quot(cb.sum(cb.quot(cb.sum(toBigDecimalProductReviewRating(cb, joinProductReview)), countProductReviewRatings),
                  cb.quot(cb.sum(joinProductDefaultRating.get(ProductDefaultRating_.DEFAULT_AVG_RATING)), countProductDefaultRatings)), 2).as(Double.class);
      }
  
      CriteriaBuilder.Coalesce&lt;Object&gt; coalesce = cb.coalesce();
      coalesce.value(countProductReviewRatings);
      coalesce.value(countProductDefaultRatings);
      coalesce.value(0);
      return coalesce.as(Double.class);
  }
  </code></pre>
  <p>The main purpose is to count average rating that depend on are they present or not and to order by this average rating</p>