tags:

views:

142

answers:

2

I'm running an application in JBoss and Using JPA.

For a report I need a group by query which I expect to return a result set with the following structure example:

count,idA,idB

I did not find a way to implement this in JPA.

What are my best options for implementing this considering I'm developing in JBoss 5, EJB3

+2  A: 

You can use a custom holder class and use the NEW keyword in your query:

SELECT NEW com.mycompany.myapp.MyClass(count, idA, idB)
FROM ...
WHERE ...

Of course, MyClass needs to have the proper constructor defined.

Pascal Thivent
This will work only for JPQL queries. This means that for result sets with aggregated value columns (not part of an Entity) this will not work. My solution now for that cases is creating a dummy Entity for holding the data only. (which will never be persisted)
Rod
A: 

In the case of Native queries, you can create a dummy entity into which the result set can be mapped to (Native query will not be mapped into an Object unless its a real managed entity). The entity is a dummy as it will not be persisted and it only used for mapping the result set of the native query into this entity.

Rod