0

I use JPA 2.0 with Hibernate. If I want to select certain columns from the table using CriteriaQuery I have to declare it like this:

CriteriaQuery<Object[]> criteria = cb.createQuery(Object[].class);
Root<Event> root = criteria.from(Event.class);
Path<Long> pid = root.get(Event_.id);
Path<Date> did = root.get(Event_.eventTime);
criteria.select( cb.array(pid, did) );
criteria.where(cb.lessThan(root.get(Event_.id), 10L));
List<Object[]> evs = em.createQuery( criteria ).getResultList();
for ( Object[] ev : evs ) {
    System.out.println(ev[0] + " " + ev[1]);
}

So, I am getting an array of chosen properties. But can I in some way get a list of instances of a Event class (in my case) where not selected properites are null ?

Event:
    id: 234
    typeid: null
    eventtime: 12-03-1990
    authorid: null
etc...

That was like it when I was using a simple Criteria of Hibernate API.

While True
  • 423
  • 2
  • 15
  • So you want to reduce the output? – udalmik Aug 12 '14 at 11:24
  • Well, kind of.. I want to do SELECT id, eventtime FROM EVENTS and get not a List of arrays of Objects but the List of Event entity-classes where all unselected properties are null. – While True Aug 12 '14 at 12:39
  • Take a look at pretty the same [question](http://stackoverflow.com/questions/12618489/jpa-criteria-api-select-only-specific-columns), already answered – udalmik Aug 12 '14 at 12:59

0 Answers0