One way JPA can retrieve only specific columns is to request a Tuple object.
In your case, you need to write something like this:
CriteriaQuery<Tuple> cq = builder.createTupleQuery(); // write the Root, Path elements as usual Root<EntityClazz> root = cq.from(EntityClazz.class); cq.multiselect(root.get(EntityClazz_.ID), root.get(EntityClazz_.VERSION)); //using metamodel List<Tuple> tupleResult = em.createQuery(cq).getResultList(); for (Tuple t : tupleResult) { Long id = (Long) t.get(0); Long version = (Long) t.get(1); }
Another approach is possible if you have a class representing the result, for example T in your case. T does not have to be an Entity class. If T has a constructor like:
public T(Long id, Long version)
then you can use T directly in your CriteriaQuery constructor:
CriteriaQuery<T> cq = builder.createQuery(T.class); // write the Root, Path elements as usual Root<EntityClazz> root = cq.from(EntityClazz.class); cq.multiselect(root.get(EntityClazz_.ID), root.get(EntityClazz_.VERSION)); //using metamodel List<T> result = em.createQuery(cq).getResultList();
See the link for more details.
perissf Oct 03 '12 at 5:35 2012-10-03 05:35
source share