title: How to properly determine whether an "exists" JPA Criteria Query clause returned true or false?
body: <p>I don't know how to perform a JPA criteria query that returns with a boolean output.</p>
  
  <p>The goal is to have a criteria query that looks like this when rendered on Oracle:</p>
  
  <pre><code>select 1 from dual where exists ( ... );
  </code></pre>
  
  <p>The <code>where exists (...)</code> part I performed with a subquery. I'm struggling with the external query.</p>
  
  <p>The practical use of this is to determine whether that subquery in the <code>exists</code> clause returns <code>true</code> or <code>false</code>.</p>
  
  <p>This is what I've written:</p>
  
  <pre><code>CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
  
  CriteriaQuery&lt;Object&gt; query = criteriaBuilder.createQuery();
  query.from(Boolean.class);
  query.select(criteriaBuilder.literal(true));
  
  Subquery&lt;Location&gt; subquery = query.subquery(Location.class);
  Root&lt;Location&gt; subRootEntity = subquery.from(Location.class);
  subquery.select(subRootEntity);
  
  Path&lt;?&gt; attributePath = subRootEntity.get("State");
  Predicate predicate = criteriaBuilder.equal(attributePath, criteriaBuilder.literal("TX"));
  subquery.where(predicate);
  query.where(criteriaBuilder.exists(subquery));
  
  TypedQuery&lt;Object&gt; typedQuery = em.createQuery(query);
  </code></pre>
  
  <p><br>
  The last line outputs an error, stating that "<em>Boolean is not an entity</em>". I think my issue is not knowing how to express the "<em>from</em>" part of the query so that the result outputs 1 or 0/ true or false - not an entity.</p>
  
  <p>I know I could retrieve any entity and then check if the list of results has size of 1. </p>
  
  <p>I'm asking how to get a boolean result, both to avoid the unnecessary task of retrieving those columns and also to learn how to do it.</p>
  
  <p><strong>Is this possible at all?</strong></p>
  
  <p>Thanks!
  Eduardo</p>