User's Manual

74 Chapter 9. Persistence Tutorial
association has a Multiplicity of 0..1 or 1..1 (or any upper bound = 1) then developers should use
the setAssociation(String attr, DomainObject dobj) method.
9.3.5.2. Using Role References
Role References can be treated in exactly the same way as standard associations. The only practical
difference between Role References and standard associations is that Role References are one-way
associations and standard associations are two-way associations. Thus, everything outlined in Section
9.3.5 Using Java to Access Associations also applies to Role References.
9.3.5.3. Using Copmposite Associations
Composite Associations are also similar to standard associations. The main difference is that in a
composite association, if one item is deleted, the other does not have any real meaning (e.g., if you
delete an Article, the Paragraph is meaningless).
Again, these can be accessed exactly as associations except for one significant difference: when the
association between an object and its component is deleted, the component is also deleted. For ex-
ample, if the association between an Article and a Paragraph were deleted, the Paragraph would be
deleted. Also, when the Article is deleted, the Paragraph is deleted.
9.4. Named SQL Events
So far, we have outlined how to interact with the database in a controlled, structured fashion. While
creating standard data objects and associations handles most developing needs, sometimes a developer
needs to perform database queries that do not fit within the standard realm of objects and associations.
It is also often the case that developers are able to perform operations in a single operation that would
normally take the system multiple operations. This situation has been handled in two separate ways
through the introduction of Data Queries (for selects) and Data Operations (for DML).
9.4.1. Data Queries
Developers often come across situations in which they need information from the database and the
persistence layer does not quite do what is needed. Therefore, the system has the ability to execute
arbitrary queries through the use of a DataQuery.
9.4.1.1. Retrieving Information from the Database
Executing arbitrary queries through DataQueries is easy. You can retrieve them in the same way
that you retrieve an existing data object, and you can execute the query and loop through the results
in the same way that you use a DataAssociationCursor.
To begin, you retrieve a query through the Session object using its model and name. Then, you can
use next() to loop through the results.
For example, if you want all paragraphs that show up in the magazine with issue number 5A. The first
step is to define the query within the PDL file. A DataQuery definition has four sections. It begins
with the declaration of the name of the query followed by data type mappings for each returned
attribute. It concludes with two code blocks. The first block, the DO block, contains the actual SQL
that will be executed. The second block, the MAP block, allows the developer to map database columns
to attribute names. The attributes are the values that can be accessed from the Java code.