I’m using JPA for a project and in most cases, want to get entities, but there are a few cases (reporting being one of them, but there are others) where I do not want or need to get entities, but rather want a selection of values. Does JPA support this? If so, does it make sense to use it or does it make sense to use straight JDBC in these cases?
Share
Q1:
Yes, it does.
It’s so called scalar queries, e.g.:
as opposed to
Q2:
When it comes to sense of using it:
Such queries have sense – you just simply need a single property not the whole object. But if you plan to make all your queries only fetching separate values, then the question ‘why?’ seems valid. The whole idea of O/R mappers is to allow you operate on objects, not (related) tables and ids. So fetching ids using JPA usually doesn’t make sense. Once you have objects mapped to database, it should be easier for you to operate on the data.