Using JPA 2.0 and EclipseLink impl
For the first question: list of custom objects(no table objects):
answer: create a custom model and use the @Entity and @Id
@Entity
public class QueryModelDTO implements Serializable{
@Id
private Integer categoryId;
private int count;
---gets and sets
}
create the query and execute
QueryModelDTO qm = (QueryModelDTO) em.createQuery(
"SELECT p.category.id as categoryId, count(p.id) as count FROM Product p
left join p.category c WHERE p.seller.id=:id
GROUP BY c.id",QueryModelDTO.class)
.setParameter("id", id).getSingleResult();
For the second: how to read the response on a map
answer: Use the QueryHints and ResultTypes (this is one variant for the @DannyMo answer)
Query q = em.createNativeQuery("SELECT * FROM Foo f");
q.setHint(QueryHints.RESULT_TYPE, ResultType.Map);
List<Map> lm = q.getResultList();
for (Map map : lm) {
for (Object entry : map.entrySet()) {
Map.Entry<DatabaseField, Object> e = (Map.Entry<DatabaseField, Object>) entry;
DatabaseField key = e.getKey();
Object value = e.getValue();
log.debug(key+"="+value);
}
}
I hope this helps