When a native SQL query returns objects, the SQL must ensure it returns the correct data to build the resultClass using the correct column names as specified in the mappings. If the SQL is more complex and returns different column names, or returns data for multiple objects then a @SqlResultSetMapping must be used.
JPA 2.0, still contains only a subset of the features supported by many database vendors
features not supported in JP QL.
performance required by an application is to replace the JP QL query with a hand-optimized SQL version. This may be a simple restructuring of the query that the persistence provider was generating, or it may be a vendor-specific version that leverages query hints and features specific to a particular database.
recommend avoiding SQL initially if possible and then introducing it only when necessary
benefits of SQL query support is that it uses the same Query interface used for JP QL queries. With some small exceptions that will be described later, all the Query interface operations discussed in previous chapters apply equally to both JP QL and SQL queries.
keep application code consistent because it needs to concern itself only with the EntityManager and Query interfaces.
An unfortunate result of adding the TypedQuery interface in JPA 2.0 is that the createNativeQuery() method was already defined in JPA 1.0 to accept a SQL string and a result class and return an untyped Query interface
consequence is that when the createNativeQuery() method is called with a result class argument one might mistakenly think it will produce a TypedQuery, like createQuery() and createNamedQuery() do when a result class is passed in.
@NamedNativeQuery
resultClass=Employee.class
The fact that the named query was defined using SQL instead of JP QL is not important to the caller
SQL Result Set Mapping
JPA provides SQL result set mappings to handle these scenarios
A SQL result set mapping is defined using the @SqlResultSetMapping annotation. It may be placed on an entity class and consists of a name (unique within the persistence unit) and one or more entity and column mappings.
expected result type and therefore received an instance of TypedQuery that is bound to the expected type. By qualifying the result type in this way, the getResultList() and getSingleResult() methods return the correct types without the need for casting.
Defining a Class for Use in a Constructor Expression
public EmpMenu(String employeeName, String departmentName)
List<EmpMenu>
NEW example.EmpMenu(" +
"e.name, e.department.name)
EmpMenu.class
createNamedQuery() can return a TypedQuery whereas the createNativeQuery() method returns an untyped Query