Red Hat Web Application Framework 6.1 用户手册

下载
页码 230
76
Chapter 9. Persistence Tutorial
The PDL is simply a Data Query with extra Attribute definitions (remove the "
\
" and make it all one
line).
query MagazineToAuthorMapping {
// the next two lines are declaring that objects will be returned
Magazine magazine;
Author author;
do {
select publications.name, issue_number, publication_id,
authors.first_name, authors.last_name, author_id
from magazines, publications, articles, authors,
magazine_article_map, article_author_map
where publications.publication_id = magazines.magazine_id
and magazine_article_map.magazine_id = magazines.magazine_id
and maagazine_article_map.article_id = article_author_map.\
article_id
and article_author_map.author_id = authors.author_id
} map {
// here we map the attributes of the objects to columns returned
// by the query.
magazine.name = publications.name;
magazine.issueNumber = magazines.issue_number;
magazine.id = publications.publication_id;
author.authorID = authors.author_id;
author.firstName = authors.first_name;
author.lastName = authors.last_name;
}
}
This can then be accessed with the Java like most other queries. The
\
marks where the line has been
wrapped for printing purposes.
DataQuery query = SessionManager.getSession().retrieveQuery\
("tutorial.MagazineToAuthorMapping");
// the next line adds the filter so that we only get author’s whose last
// name begins with the letter "s".
Note that we are using a stanard filter
// because we need to perform a function on the column and we are positive
// that the value is not null.
Filter filter = query.addFilter("lower(lastName) like \
’%’ || :lastNamePrefix");
filter.set("lastNamePrefix", "s");
while (query.next()) {
DataObject myAuthor = query.get("author");
DataObject myMagazine = query.get("magazine");
System.out.println("the author I retrieved is " + myAuthor);
System.out.println("the magazine I retrieved is " + myMagazine);
}
9.4.2. Data Operations
As mentioned previously, developers often need to be able to execute arbitrary DML statements that
do not fit nicely into the realm of data objects and data associations. To accommodate this need,
the system contains the concept of a
DataOperation
that can be used to execute arbitrary DML
statements or PL/SQL functions and procedures.