How to get value of some fields in a native query (JPA)?
For example I want to get name and age of customer table:
Query q = em.createNativeQuery("SELECT name,age FROM customer WHERE id=...");
Note: I don't want to map results to entities. I just want to get the value of the field.
Thanks
A native query with multiple select expressions returns an
Object[]
(orList<Object[]>
). From the specification:So, to get the name and age in your example, you'd have to do something like this:
References
Depends on the JPA implementation. Hibernate does it different than castor, for example. Here's a example how it would work in Hibernate:
I don't think that it is possible to get a plane value such as an integer... but this one should come very close to it.