Native queries executed with Query result and Object always until you define the resultClass property in the createNativeQuery method.
createNativeQuery
Query createNativeQuery(java.lang.String sqlString,
java.lang.Class resultClass)
Create an instance of Query for executing a native SQL query.
Parameters:
sqlString - a native SQL query string
resultClass - the class of the resulting instance(s)
If you have a entity with the same name of the result fields of the query, add resultClass=Class.class and will create objects automatically
Are you are using a join between 2 tables, you can use @SqlResultSetMapping as it follows.
@SqlResultSetMapping(name="ScalarAndEntities",
entities={
@EntityResult(name="org.hibernate.test.annotations.query.Night", fields = {
@FieldResult(name="id", column="nid"),
@FieldResult(name="duration", column="night_duration"),
@FieldResult(name="date", column="night_date"),
@FieldResult(name="area", column="area_id")
}),
@EntityResult(name="org.hibernate.test.annotations.query.Area", fields = {
@FieldResult(name="id", column="aid"),
@FieldResult(name="name", column="name")
})
},
columns={
@ColumnResult(name="durationInSec")
}
)
Use with
Query q = entityManager.createNativeQuery(sqlQuery, "ScalarAndEntities");
Read more about how to retrieve entities from nativequeries here ,