Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>One of the JPA ways for getting only particular columns is to ask for a <a href="http://docs.oracle.com/javaee/6/api/javax/persistence/Tuple.html">Tuple</a> object.</p> <p>In your case you would need to write something like this:</p> <pre><code>CriteriaQuery&lt;Tuple&gt; cq = builder.createTupleQuery(); // write the Root, Path elements as usual Root&lt;EntityClazz&gt; root = cq.from(EntityClazz.class); cq.multiselect(root.get(EntityClazz_.ID), root.get(EntityClazz_.VERSION)); //using metamodel List&lt;Tuple&gt; tupleResult = em.createQuery(cq).getResultList(); for (Tuple t : tupleResult) { Long id = (Long) t.get(0); Long version = (Long) t.get(1); } </code></pre> <p>Another approach is possible if you have a class representing the result, like <code>T</code> in your case. <code>T</code> doesn't need to be an Entity class. If <code>T</code> has a constructor like:</p> <pre><code>public T(Long id, Long version) </code></pre> <p>then you can use <code>T</code> directly in your <code>CriteriaQuery</code> constructor:</p> <pre><code>CriteriaQuery&lt;T&gt; cq = builder.createQuery(T.class); // write the Root, Path elements as usual Root&lt;EntityClazz&gt; root = cq.from(EntityClazz.class); cq.multiselect(root.get(EntityClazz_.ID), root.get(EntityClazz_.VERSION)); //using metamodel List&lt;T&gt; result = em.createQuery(cq).getResultList(); </code></pre> <p>See this <a href="http://www.ibm.com/developerworks/java/library/j-typesafejpa/">link</a> for further reference.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload