title: In jpa criteria, "in case there is at least 1 row return true"
body: <p>I'm trying to create the follow sentence using the criteria api in JPA (eclipselink), 
  it simple ask if there exist some user in some category</p>
  
  <p>The sentence I want:</p>
  
  <pre><code>SELECT 
    CASE 
       WHEN EXISTS
            (SELECT * FROM user WHERE category = ?)
       THEN true
       ELSE false
    END 
  bind =&gt; [10]
  </code></pre>
  
  <p>I trying using this code:</p>
  
  <pre><code>CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
  CriteriaQuery&lt;Boolean&gt; criteriaQuery = criteriaBuilder.createQuery(Boolean.class);
  Root&lt;T&gt; root = criteriaQuery.from(tclass);
  
  Subquery&lt;T&gt; subquery = criteriaQuery.subquery(tclass);
  Root&lt;T&gt; subroot = subquery.from(tclass);
  subquery.select(subroot);
  
  Predicate subPredicate = criteriaBuilder.equal(subroot.get("category"),category);
  subquery.where(subPredicate);
  
  Predicate predicateExists = criteriaBuilder.exists(subquery);
  
  Case&lt;Boolean&gt; booleancase = criteriaBuilder.&lt;Boolean&gt;selectCase();
  Expression&lt;Boolean&gt; booleanExpression =
      booleancase.when(predicateExists,true)
      .otherwise(false);
  
  criteriaQuery.select(booleanExpression);
  
  TypedQuery&lt;Boolean&gt; typedQuery = entityManager.createQuery(criteriaQuery);
  typedQuery.getResultList();
  </code></pre>
  
  <p>Sadly the sentence I have is the follow, I want to erase the last "from user":</p>
  
  <pre><code>SELECT 
    CASE 
        WHEN EXISTS
            (SELECT ? FROM user t1 WHERE (t1.category = ?))  
       THEN ?
       ELSE ? 
    END
  FROM user t0    
  bind =&gt; [1, 110, true, false]
  </code></pre>
  
  <p>Any idea?</p>