I am using JPA in my project.
I came to a query in which I need to make join operation on five tables. So I created a native query which returns five fields.
Now I want to convert the result object to java POJO class which contains the same five Strings.
Is there any way in JPA to directly cast that result to POJO object list ??
I came to the following solution ..
@NamedNativeQueries({
@NamedNativeQuery(
name = "nativeSQL",
query = "SELECT * FROM Actors",
resultClass = db.Actor.class),
@NamedNativeQuery(
name = "nativeSQL2",
query = "SELECT COUNT(*) FROM Actors",
resultClass = XXXXX) // <--------------- problem
})
Now here in resultClass, do we need to provide a class which is actual JPA entity ? OR We can convert it to any JAVA POJO class which contains the same column names ?
Yes, with JPA 2.1 it's easy. You have very useful Annotations. They simplify your life.
First declare your native query, then your result set mapping (which defines the mapping of the data returned by the database to your POJOs). Write your POJO class to refer to (not included here for brevity). Last but not least: create a method in a DAO (for example) to call the query. This worked for me in a dropwizard (1.0.0) app.
First declare a native query in an entity class:
Underneath you can add the resultset mapping declaration:
Later in a DAO you can refer to the query as
That's it.
First declare following annotations:
Then annotate your POJO as follows:
Then write annotation processor:
Use above framework as follows:
If you use
Spring-jpa
, this is a supplement to the answers and this question. Please correct this if any flaws. I have mainly used three methods to achieve "mapping resultObject[]
to a pojo" based on what practical need I meet:sql
with itsEntity
are enough.The former 2 failed, and I have to use a
nativeQuery
. Here are the examples. The pojo expected:Method 1: Change the pojo into an interface:
And repository:
Method 2: Repository:
Note: parameter sequence of POJO constructor must be identical in both POJO definition and sql.
Method 3: Use
@SqlResultSetMapping
and@NamedNativeQuery
inEntity
as the example in Edwin Dalorzo's answer.The first two methods would call many in-the-middle handlers, like customized converters. For example,
AntiStealing
defines asecretKey
, before it is persisted, a converter is inserted to encrypt it. This would result in the first 2 methods returning a converted backsecretKey
which is not what I want. While the method 3 would overcome the converter, and returnedsecretKey
would be the same as it is stored (an encrypted one).See example below for using a POJO as pseudo entity to retrieve result from native query without using complex SqlResultSetMapping. Just need two annotations, a bare @Enity and a dummy @Id in your POJO. @Id can be used on any field of your choice, an @Id field can have duplicate keys but not null values.
Since @Enity does not map to any physical table, so this POJO is called a pseudo entity.
Environment: eclipselink 2.5.0-RC1, jpa-2.1.0, mysql-connector-java-5.1.14
You can download complete maven project here
Native query is based on mysql sample employees db http://dev.mysql.com/doc/employee/en/employees-installation.html
persistence.xml
Employee.java
EmployeeNativeQuery.java
Since others have already mentioned all the possible solutions, I am sharing my workaround solution.
In my situation with
Postgres 9.4
, while working withJackson
,I am sure you can find same for other databases.
Also FYI, JPA 2.0 native query results as map
Simple way to converting SQL query to POJO class collection ,