title: how to create predicate of the jpa specification or criteria query for the below query
body: <p>I am trying to create JPA specification for with filter data between two dates date1 and date2, I tried to create through subquery but no luck as of now, please help if possible.
  I am trying to create the specification for the below SQL query,</p>
  
  <pre><code>select
      * from
      subscription sub join (
          select subscription_id,
          max(output_date) as max_output_date
      from
          subscription_package
      where
          package_status = 'COMPLETE'
      group by
          subscription_id) sp on
      sub.id = sp.subscription_id where
      max_output_date between 'date1' and 'date2'
  </code></pre>
  
  <p>Below is the query created in JPA:</p>
  
  <pre><code>            Subquery&lt;Date&gt; datequery = query
              .subquery(Date.class);
          Root&lt;SubscriptionPackage&gt; dateRoot = datequery
              .from(SubscriptionPackage.class);
  
          Subquery&lt;SubscriptionPackage&gt; subquery = query
              .subquery(SubscriptionPackage.class);
          Root&lt;SubscriptionPackage&gt; subqueryRoot = subquery
              .from(SubscriptionPackage.class);
  
          datequery.select(criteriaBuilder
              .greatest(subqueryRoot.&lt;Date&gt;get("outputDate")));
          datequery.where(criteriaBuilder.equal(subqueryRoot.get("PackageStatus"),
                  'COMPLETE'));
          datequery.groupBy(subqueryRoot.get("subscription"));
  </code></pre>
  
  <p>not able to use the result from date query for the comparison like </p>
  
  <pre><code>query.where(criteriaBuilder.between(datequery., outFrom, outTo));
  </code></pre>