2.12.1. Creating SQL Queries
The EntityManager has two factory methods suitable for creating SQL queries:
public Query createNativeQuery(String sqlString, Class resultClass);
public Query createNativeQuery(String sqlString, String resultSetMapping);
The first method is used to create a new Query instance that will return instances of the specified class.
The second method uses a SqlResultSetMapping to determine the type of object or objects to return.
The example below shows these methods in action.
Example 21. Creating a SQL Query
EntityManager em = ...;
Query query = em.createNativeQuery("SELECT * FROM MAG", Magazine.class);
processMagazines(query.getResultList());
|
|
In addition to SELECT statements, OpenJPA supports stored procedure invocations as SQL queries.
OpenJPA will assume any SQL that does not begin with the |