Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

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

(Native SQL Queries)
Line 1: Line 1:
 
{{EclipseLink_UserGuide
 
{{EclipseLink_UserGuide
 
|info=y
 
|info=y
|toc=n
+
|toc=y
 
|eclipselink=y
 
|eclipselink=y
 
|eclipselinktype=JPA
 
|eclipselinktype=JPA
Line 57: Line 57:
 
Parameters to SQL queries are delimited using the <tt>?</tt> character.  Only indexed parameters are supported, named parameters are not supported.
 
Parameters to SQL queries are delimited using the <tt>?</tt> character.  Only indexed parameters are supported, named parameters are not supported.
 
The index can be used in the delimiter, i.e. <tt>?1</tt>.  Parameter values are set on the <tt>Query</tt> using the <tt>setParameter</tt> API.
 
The index can be used in the delimiter, i.e. <tt>?1</tt>.  Parameter values are set on the <tt>Query</tt> using the <tt>setParameter</tt> API.
Indxed parameter start at the index <tt>1</tt> not <tt>0</tt>.
+
Indexed parameters start at the index <tt>1</tt> not <tt>0</tt>.
  
 
=====''Native SQL query parameter example''=====
 
=====''Native SQL query parameter example''=====
Line 68: Line 68:
  
 
==SqlResultSetMapping==
 
==SqlResultSetMapping==
 +
An <tt>@SqlResultSetMapping</tt> 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.  <tt>@EntityResult</tt> and <tt>@FieldResult</tt> are used to map the SQL query result column to the entity attribute.  <tt>@ColumnResult</tt> can be used to add a data element to the result.
 +
 +
<tt>SqlResultSetMappings</tt> are defined through annotations or XML.  There are referenced from SQL queries by name.
 +
 +
=====''SqlResultSetMapping example''=====
 +
<source lang="java">
 +
@SqlResultSetMapping(
 +
)
 +
@NamedQuery(query="")
 +
@Entity
 +
public class Employee {
 +
  ...
 +
}
 +
Query query = em.createNativeQuery("SELECT * FROM EMPLOYEE WHERE F_NAME = ? AND L_NAME = ?", Employee.class);
 +
query.setParameter(1, "Bob");
 +
query.setParameter(2, "Smith");
 +
List<Employee> result = query.getResultList();
 +
</source>
  
 
{{EclipseLink_JPA
 
{{EclipseLink_JPA

Revision as of 11:18, 18 June 2012

EclipseLink JPA

Eclipselink-logo.gif
EclipseLink
Website
Download
Community
Mailing ListForumsIRCmattermost
Issues
OpenHelp WantedBug 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 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 correct column names that the entity's mappings expect, or an SqlResultSetMapping can be used. An SqlResultSetMapping allows for 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, for SQL queries that return results, getSingleResult or getResultList can be used, for SQL queries that do not 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 certain complex stored procedures, cannot be execute with SQL queries. EclipseLink supports stored procedures through stored procedure queries, for more information see Stored Procedures.

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

  • eclipselink.batch
  • eclipselink.history.as-of
  • eclipselink.inheritance.outer-join
  • eclipselink.sql.hint
  • eclipselink.join-fetch - join-fetch is supported, but requires that the SQL select all of the joined columns.
  • eclipselink.fetch-group - fetch-group is supported, but requires that the SQL select all of the fetched columns.
  • eclipselink.pessimistic-lock - pessimistic-lock is supported, but requires that the SQL locked the result rows.
Native SQL query examples
Query query = em.createNativeQuery("SELECT * FROM EMPLOYEE", Employee.class);
List<Employee> result = query.getResultList();
Query query = em.createNativeQuery("SELECT SYSDATE FROM DUAL");
Date result = (Date)query.getSingleResult();
Query query = em.createNativeQuery("SELECT MAX(SALARY), MIN(SALARY) FROM EMPLOYEE");
List<Object[]> results = query.getResultList();
Query query = em.createNativeQuery("DELETE FROM EMPLOYEE");
int rowCount = query.executeUpdate();

Parameters

Parameters to SQL queries are delimited using the ? character. Only indexed parameters are supported, named parameters are not supported. The index can be used in the delimiter, i.e. ?1. Parameter values are set on the Query using the setParameter API. Indexed parameters start at the index 1 not 0.

Native SQL query parameter example
Query query = em.createNativeQuery("SELECT * FROM EMPLOYEE WHERE F_NAME = ? AND L_NAME = ?", Employee.class);
query.setParameter(1, "Bob");
query.setParameter(2, "Smith");
List<Employee> result = query.getResultList();

SqlResultSetMapping

An @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. @EntityResult and @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. There are referenced from SQL queries by name.

SqlResultSetMapping example
@SqlResultSetMapping(
)
@NamedQuery(query="")
@Entity
public class Employee {
  ...
}
Query query = em.createNativeQuery("SELECT * FROM EMPLOYEE WHERE F_NAME = ? AND L_NAME = ?", Employee.class);
query.setParameter(1, "Bob");
query.setParameter(2, "Smith");
List<Employee> result = query.getResultList();

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

Back to the top