Red Hat Web Application Framework 6.1 用户手册

下载
页码 230
Chapter 9. Persistence Tutorial
77
9.4.2.1. Executing Arbitrary DML
Data Operations are similiar to DataQueries in both structure and use. However, while they are re-
trieved in a fashion similar to DataQueries, they are executed differently. After the query is retrieved,
the program can set bind variables, after which it is executed. Suppose you want to create a magazine
with ID 4 using all articles in the system that are not yet currently in a magazine. To do this, you could
create a new Magazine DataObject, give it an ID of 4, use a
DataQuery
to get all articles not already
in a magazine, add those articles to the magazine through the use of associations, and then save the
magazine. Alternately, you can use a
DataOperation
and execute a single query.
The
DataOperation
to execute the above query is structured in almost the same way as a
Data-
Query
. In fact, it can even have an
OPTIONS
block (although it does not yet have any valid values for
the options block). However, since it does not return many different rows of results, it does not allow
attribute mappings before the first
do
block. The PDL can be defined as follows:
data operation createMagazine {
do {
insert into magazine_article_map (magazine_id, article_id)
select :magazineID, article_id from articles where not exists
(select 1 from magazine_article_map
where magazine_article_map.article_id = articles.article_id)
}
}
Now that the operation is defined, you can set the value of the bind variable
magazineID
to the
correct value and then execute the operation. This can be done with code such as the following (the
\
marks where the line has been wrapped for printing purposes):
DataOperation operation = getSession().retrieveDataOperation\
("tutorial.createMagazine");
// we have to pass in an Integer instead of an int so that JDBC can
// handle it correctly
operation.setParameter("magazineID", new Integer(4));
operation.execute();
9.4.2.2. Executing PL/SQL
Developers often need to execute PL/SQL procedures and functions. Therefore, it is possible to exe-
cute both using a
DataOperation
with additional syntax. Arguments can be passed to functions ans
procedures using Parameter Binding.
Note
The methods described below do not allow users to return cursors from their PL/SQL functions. If
this is required, the recommended workaround is to use a
CallableStatement
directly and bypass
the persistence layer entirely.
9.4.2.2.1. PL/SQL Procedures
Suppose you want to execute the following PL/SQL procedure:
create or replace function myPLSQLProc(v_priority in integer)
as
begin
insert into magazines (magazine_id, title)