Java Reference
In-Depth Information
even support stored procedures written in Java including Oracle, Informix, and DB2. A
stored procedure is basically a function that takes a set of parameters and can optionally
return data. The data can be returned from the function or passed back via the parameters
to the function. Parameters can be IN , OUT , or INOUT IN being a parameter passed in,
OUT being a parameter used to return a value, and INOUT being a parameter that's con-
sumed and also returns a value.
Stored procedures are used for a myriad of reasons: faster performance, data logging,
avoiding network traffic on complex queries, encapsulating business logic, handling per-
missions, and more. Stored procedures can either be invoked like a query or automatically
executed in the case of a trigger. These two methods of invoking stored procedures are the
most common, but several databases provide even more methods such as using HTTP to
invoke the stored procedure. With JPA, you can invoke a stored procedure and process the
results just like any other query.
With JPA's stored procedure support, you work with a
javax.persistence.Stored-ProcedureQuery object. This class extends the
javax.persistence.Query interface that you've seen throughout this chapter. To
create a StoredProcedureQuery , you use one of the three methods on the
EntityManager :
StoredProcedureQuery createStoredProcedureQuery(String procedureName);
StoredProcedureQuery createStoredProcedureQuery(
String procedureName, Class... resultClasses);
StoredProcedureQuery createStoredProcedureQuery(
String procedureName, String... resultSetMappings);
These methods take a procedure name as well as the result types or mappings. The proced-
ure name is the name of the procedure in the database. Table 11.13 lists the important meth-
ods on the StoredProcedureQuery interface. You use these methods to configure the
parameters that you'll send the procedure. As mentioned earlier, parameters can be used to
pass in values ( IN ), retrieve data ( OUT ), or both pass in and retrieve data ( INOUT )—re-
ferred to as the direction of the parameter. The ParameterMode is an enum that you pass
in when you're defining the parameters.
Search WWH ::

Custom Search