Jump to: navigation, search

Difference between revisions of "EclipseLink/UserGuide/JPA/Basic JPA Development/Querying/Native"

Line 7: Line 7:
 
|apis=
 
|apis=
 
* [http://www.eclipse.org/eclipselink/api/latest/javax/persistence/SqlResultSetMapping.html SqlResultSetMapping]
 
* [http://www.eclipse.org/eclipselink/api/latest/javax/persistence/SqlResultSetMapping.html SqlResultSetMapping]
 +
* [http://www.eclipse.org/eclipselink/api/latest/javax/persistence/EntityResult.html EntityResult]
 +
* [http://www.eclipse.org/eclipselink/api/latest/javax/persistence/FieldResult.html FieldResult]
 +
* [http://www.eclipse.org/eclipselink/api/latest/javax/persistence/ColumnResult.html ColumnResult]
 
|nativeapi=y
 
|nativeapi=y
 
|nativeapis=
 
|nativeapis=
Line 22: Line 25:
 
Query settings and query hints that affect the generated SQL are not supported with SQL queries.
 
Query settings and query hints that affect the generated SQL are not supported with SQL queries.
 
Unsupported query hints include:
 
Unsupported query hints include:
*  
+
* <tt>eclipselink.batch-fetch</tt>
  
 
====='Native SQL query examples'=====
 
====='Native SQL query examples'=====

Revision as of 09:28, 18 June 2012

EclipseLink JPA


Native SQL Queries

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 more comfortable in the SQL language versus 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 correct column names that the entity's mappings expect, or a ResultSetMapping can be used. A ResultSetMapping allows for the SQL result set to be mapped to an entity, or set of entities and data.

SQL queries can be to execute SQL or DML, for SQL queries that return results, getSingleResult or getResultList can be used, for SQL queries that don't return results, executeUpdate must be used. SQL queries can be used to execute database operations and some stored procedures and functions. Stored prcoedures that return output parameters, or complex stored procedure cannot be execute with SQL queries, but stored procedure queries can be used, see StoredProcedures.

Query settings and query hints that affect the generated SQL are not supported with SQL queries. Unsupported query hints include:

  • eclipselink.batch-fetch
'Native SQL query examples'
Query query = ((JpaEntityManager)entityManager.getDelegate()).createQuery(databaseQuery);
List result = query.getResultList();

Eclipselink-logo.gif
Version: 2.4 DRAFT
Other versions...