Beta Draft: 2013-04-02
JPA allows SQL to be used for querying entity objects, or data. SQL queries are not translated, and passed directly to the database. SQL queries can be used for advanced queries that require database specific syntax, or by users who are more comfortable in the SQL language than JPQL or Java.
SQL queries are created from the
EntityManager using the
createNativeQuery API or via named queries. A Query object is returned and executed the same as any other JPA query. An SQL query can be created for an entity class, or return an object array of data. If returning entities, the SQL query must return the column names that the entity's mappings expect, or an
SqlResultSetMapping can be used. An
SqlResultSetMapping allows the SQL result set to be mapped to an entity, or set of entities and data.
SQL queries can be used to execute SQL or DML (Data Manipulation Language) statements. For SQL queries that return results,
getResultList can be used. For SQL queries that do not return results,
executeUpdate must be used.
executeUpdate can only be used within a transaction. SQL queries can be used to execute database operations and some stored procedures and functions. Stored procedures that return output parameters, or certain complex stored procedures, cannot be executed with SQL queries. EclipseLink supports stored procedures through stored procedure queries.
Query settings and query hints that affect the generated SQL are not supported with SQL queries. Unsupported query hints include:
join-fetch is supported, but requires that the SQL selects all of the joined columns.
fetch-group is supported, but requires that the SQL selects all of the fetched columns.
pessimistic-lock is supported, but requires that the SQL locks the result rows.
For descriptions of these extensions, see "EclipseLink Query Language" in Java Persistence API (JPA) Extensions Reference for EclipseLink.
Parameters to SQL queries are delimited using the question mark (
?) character. Only indexed parameters are supported, named parameters are not supported. The index can be used in the delimiter, such as
?1. Parameter values are set on the Query using the
setParameter API. Indexed parameters start at the index 1 not 0.
Native SQL queries can be defined as named queries in annotations or XML using the
NamedNativeQuery annotation or
<named-native-query> XML element. Named native SQL queries are executed the same as any named query.
SqlResultSetMapping can be used to map the results of an SQL query to an entity if the result column names do not match what the entity mappings expect. It can also be used to return multiple entities, or entities and data from a single SQL query.
FieldResult are used to map the SQL query result column to the entity attribute.
ColumnResult can be used to add a data element to the result.
SqlResultSetMappings are defined through annotations or XML using the
@SqlResultSetMapping annotation or
<sql-result-set-mapping> XML element. They are referenced from native SQL queries by name.