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

From Eclipsepedia

Jump to: navigation, search
Line 3: Line 3:
 
|toc=n
 
|toc=n
 
|eclipselink=y
 
|eclipselink=y
|eclipselinktype=JPA}}
+
|eclipselinktype=JPA
 +
|api=y
 +
|apis=
 +
* [http://www.eclipse.org/eclipselink/api/latest/javax/persistence/ResultSetMapping.html ResultSetMapping]
 +
|nativeapi=y
 +
|nativeapis=
 +
* [http://www.eclipse.org/eclipselink/api/latest/org/eclipse/persistence/queries/SQLCall.html SQLCall]}}
  
 
=Native SQL Queries=
 
=Native SQL Queries=
  
In addition to [[EclipseLink/UserGuide/JPA/Basic JPA Development/Querying/JPQL|JPQL]], EclipseLink rich native DatabaseQuery and Expression criteria API.
+
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 <tt>EntityManager</tt> using the <tt>createNativeQuery</tt> API or via named queries.  A <tt>Query</tt> 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 <tt>ResultSetMapping</tt> can be used.  A <tt>ResultSetMapping</tt> 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, <tt>getSingleResult</tt> or <tt>getResultList</tt> can be used, for SQL queries that don't return results, <tt>executeUpdate</tt> 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 [[EclipseLink/UserGuide/JPA/Basic JPA Development/Querying/StoredProcedures|StoredProcedures]].
 +
 
 +
Query settings and query hints that affect the generated SQL are not supported with SQL queries.
 +
Unsupported query hints include:
 +
*
  
 
=== Using JpaEntityManager createQuery() API ===
 
=== Using JpaEntityManager createQuery() API ===

Revision as of 10:23, 18 June 2012

EclipseLink JPA

link="http://wiki.eclipse.org/EclipseLink"
EclipseLink
Website
Download
Community
Mailing ListForumsIRC
Bugzilla
Open
Help Wanted
Bug Day
Contribute
Browse Source

Elug api package icon.png Native API


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:

Using JpaEntityManager createQuery() API

import javax.persistence.Query;
import org.eclipse.persistence.expressions.*;
import org.eclipse.persistence.queries.ReadAllQuery;
 
ExpressionBuilder builder = new ExpressionBuilder();
ReadAllQuery databaseQuery = new ReadAllQuery(Employee.class, builder);
databaseQuery.setSelectionCriteria(builder.get("firstName").like("B%"));
databaseQuery.addOrdering(builder.get("firstName").toUpperCase());
 
Query query = ((JpaEntityManager)entityManager.getDelegate()).createQuery(databaseQuery);
List result = query.getResultList();

Elug note icon.png

Note: The JpaEntityManager API was added in EclipseLink 1.1.

In EclipseLink 1.0, the JpaQuery method setDatabaseQuery() could be used.


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