You could use a projection. If you are using Spring Data, it includes some nice helpers see Baeldung: Spring Data JPA Projections and Spring Data JPA Reference for details. If you are using Hibernate directly, see Baeldung: JPA/Hibernate Projection and Hibernate JavaDoc Projection
Hibernate (direct quote from Baeldung)
To project on multiple columns using JPQL, we only have to add all the required columns to the select clause:
Query query = session.createQuery("select id, name, unitPrice from Product");
List resultList = query.getResultList();
But, when using a CriteriaBuilder, we'll have to do things a bit differently:
CriteriaBuilder builder = session.getCriteriaBuilder();
CriteriaQuery<Object[]> query = builder.createQuery(Object[].class);
Root product = query.from(Product.class);
query.multiselect(product.get("id"), product.get("name"), product.get("unitPrice"));
List<Object[]> resultList = entityManager.createQuery(query).getResultList();
Here, we've used the method multiselect() instead of select(). Using this method, we can specify multiple items to be selected.
Another significant change is the use of Object[]. When we select multiple items, the query returns an object array with value for each item projected. This is the case with JPQL as well.
Let's see what the data looks like when we print it:
[1, Product Name 1, 1.40]
[2, Product Name 2, 4.30]
[3, Product Name 3, 14.00]
[4, Product Name 4, 3.90]
As we can see, the returned data is a bit cumbersome to process. But, fortunately, we can get JPA to populate this data into a custom class.
Also, we can use CriteriaBuilder.tuple() or CriteriaBuilder.construct() to get the results as a list of Tuple objects or objects of a custom class respectively.
I suggest reading the Baeldung article, it goes into more detail.