Use Store Procedure in JPA
2013-02-20 21:29
190 查看
Usually, we use JDBC to call store procedure and it works well.However, i found that it is hard to call store procedure when i used OpenJPA in my project.Frankly speaking, i try my best to think up possible solutions like using EntityManager's native query
or getting the Connection object from OpenJPA API, but it is still not so convenient and lots of trouble indeed.I began to doubt the JPA specification and implementation, then i read the JPA specification totally. I am excited to note that JPA 2.1 specification
draft the store procedure support.
Store procedure is highlighted in JPA 2.1 specification:
Support for Stored Procedures: Added support for the invocation of predefined database functions and user-defined database functions to the Java Persistence query language.
There are different variants of EntityManager.createXXXStoredProcedureQuery methods that return aStoredProcedureQuery for executing a stored procedure. Just liked @NamedQuery, there is@NamedStoredProcedureQuery that specifies and names a stored procedure,
its parameters, and its result type. This annotation can be specified on an entity or mapped superclass. The name specified in the annotation is then used in EntityManager.createNamedStoredProcedureQuery. The IN, OUT, and INOUT parameters can be set and used
to retrieve values passed back from the procedure.
Moreover, EclipseLink is the best one of JPA implementations which supports store procedure.
EclipseLink has extended support for stored procedure execution including:
IN, OUT and INOUT parameter support
CURSOR output parameter support
Result set support
Annotation support using @NamedStoredProcedureQuery
Dynamic definition support using StoreProcedureCall
Stored function support using StoredFunctionCall
Object-relational data-types, Struct, Array (OBJECT types, VARRAY), including mapping using ObjectRelationalDataTypeDescriptor
PLSQL types, BOOLEAN, RECORD, TABLE, using PLSQLStoredProcedureCall
A stored procedure call be used in any query to read objects, or read or modify raw data.
Any CRUD or mapping operation can also be overridden using a stored procedure call using a DescriptorCustomizer and the DescriptorQueryManager API.
Oracle stored procedure using OUT CURSOR
Using JpaEntityManager createQuery() API to execute a stored procedure
Using @NamedStoredProcedureQuery to define a stored procedure
or getting the Connection object from OpenJPA API, but it is still not so convenient and lots of trouble indeed.I began to doubt the JPA specification and implementation, then i read the JPA specification totally. I am excited to note that JPA 2.1 specification
draft the store procedure support.
Store procedure is highlighted in JPA 2.1 specification:
Support for Stored Procedures: Added support for the invocation of predefined database functions and user-defined database functions to the Java Persistence query language.
There are different variants of EntityManager.createXXXStoredProcedureQuery methods that return aStoredProcedureQuery for executing a stored procedure. Just liked @NamedQuery, there is@NamedStoredProcedureQuery that specifies and names a stored procedure,
its parameters, and its result type. This annotation can be specified on an entity or mapped superclass. The name specified in the annotation is then used in EntityManager.createNamedStoredProcedureQuery. The IN, OUT, and INOUT parameters can be set and used
to retrieve values passed back from the procedure.
Moreover, EclipseLink is the best one of JPA implementations which supports store procedure.
EclipseLink has extended support for stored procedure execution including:
IN, OUT and INOUT parameter support
CURSOR output parameter support
Result set support
Annotation support using @NamedStoredProcedureQuery
Dynamic definition support using StoreProcedureCall
Stored function support using StoredFunctionCall
Object-relational data-types, Struct, Array (OBJECT types, VARRAY), including mapping using ObjectRelationalDataTypeDescriptor
PLSQL types, BOOLEAN, RECORD, TABLE, using PLSQLStoredProcedureCall
A stored procedure call be used in any query to read objects, or read or modify raw data.
Any CRUD or mapping operation can also be overridden using a stored procedure call using a DescriptorCustomizer and the DescriptorQueryManager API.
Oracle stored procedure using OUT CURSOR
CREATE PROCEDURE EMP_READ_ALL ( RESULT_CURSOR OUT CURSOR_TYPE.ANY_CURSOR) AS BEGIN OPEN RESULT_CURSOR FOR SELECT e.*, s.* FROM EMPLOYEE e, SALARY s WHERE e.EMP_ID = s.EMP_ID; END;
Using JpaEntityManager createQuery() API to execute a stored procedure
import javax.persistence.Query; import org.eclipse.persistence.queries.StoredProcedureCall; import org.eclipse.persistence.queries.ReadAllQuery; ReadAllQuery databaseQuery = new ReadAllQuery(Employee.class); StoredProcedureCall call = new StoredProcedureCall(); call.setProcedureName("EMP_READ_ALL"); call.useNamedCursorOutputAsResultSet("RESULT_CURSOR"); databaseQuery.setCall(call); Query query = ((JpaEntityManager)entityManager.getDelegate()).createQuery(databaseQuery); List<Employee> result = query.getResultList();
Using @NamedStoredProcedureQuery to define a stored procedure
@NamedStoredProcedureQuery(name="findAllEmployees", procedureName="EMP_READ_ALL", resultClass=Employee.class, parameters={ @StoredProcedureParameter(queryParameter="RESULT_CURSOR", name="result", direction=Direction.OUT_CURSOR)}) @Entity public class Employee { ... }Using named query
Query query = entityManager.createNamedQuery("findAllEmployees"); List<Employee> result = query.getResultList();
相关文章推荐
- Use Stored Procedure in JPA
- use AP_VENDOR_PUB_PKG.Update_Vendor_Site_Public to u ORA-01722: invalid number in Package AP_VENDOR_PUB_PKG Procedure Update_Vendor_Site_Public
- How To Use a DataReader Against an Oracle Stored Procedure in Visual C# .NET
- How to Use a Function or a Procedure as a Parameter in another Function
- Why use Store procedure
- Exception in thread "main" java.lang.RuntimeException: Hive metastore database is not initialized. Please use schematool (e.g. ./schematool -initSchema -dbType ...) to create the schema. If needed, do
- How to Add/Update Store Procedure in EF
- how to debug store procedure in sql server 2005
- spring.data.jpa.query :Cannot use native queries with dynamic sorting and/or pagination in method
- [Debug] Use Snippets to Store Behaviors in Chrome DevTools
- Learning Store procedure and debug in SQL Server 2008
- How to use Oracle 11g ODP.NET UDT in an Oracle Stored Procedure's Where clause
- iOS7 BUG:Your account is not valid for use in the ***Store...
- How to use Oracle 11g ODP.NET UDT in an Oracle Stored Procedure's Where clause
- ow to use DllMain in a MFC dll project (ZZ
- How to Use Buffers in Node.js
- Discussion: How Integer store in Storage ?
- Avoid to use "IN", "NOT IN" in SQL statement, try to use "LEFT JOIN" instead.
- MyEclipse默认目录修改及Workspace in use or cannot be created, choose a different one错误解决方法
- use case in Functional Specification