User's Manual

72 Chapter 9. Persistence Tutorial
}
9.3.3. Role References
Developers ofen only need to be able to obtain associated information in a single direction. For in-
stance, if authors have screen names that are used and can be shared, it is useful to be able to look
up the screen name for a given author. However, it may not be as important to look up the author
that corresponds to a given screen name. In this case, developers should use a Role Reference. In the
following example, the developer wants to be able to easily look up a given screen name for an author.
The PDL below can be used to create Object Types for both ScreenName and Author. Notice that
Author contains a role reference to a ScreenName.
model tutorial;
object type ScreenName {
BigDecimal id = screen_names.name_id INTEGER;
String screenName = screen_names.screen_name VARCHAR(200);
Blob screenIcon = screen_names.screen_icon BLOB;
object key (id);
}
object type Author {
BigInteger[1..1] id = authors.author_id INTEGER;
String[1..1] firstName = author.first_name VARCHAR(700);
String[1..1] lastName = author.last_name VARCHAR(700);
Blob[0..1] portrait = authors.portrait BLOB;
// the following line is the role reference. Notice that it
// appears in the definition just like an Attribute. The only
// difference is that instead of pointing to a column in the
ScreenName[0..1] screenName =
join authors.screen_name_id to screen_names.name_id;
object key (id);
}
9.3.4. Link Attributes
One final feature that is immensely useful for associating objects is the idea of Link Attributes. Often,
some sort of relationship is needed for associations. For instance, for Magazines, it is useful to include
the page number with the Article. The concept of having Articles associated with Magazines is
covered by standard associations but in order to capture a page number with the association, Link
Attributes are needed.
// this is an "association block" associating "articles" and "magazines"
association {
Article[0..n] articles = join magazines.magazine_id
to magazine_article_map.magazine_id,
join magazine_article_map.article_id
to articles.article_id;
Magazine[0..n] magazines = join articles.article_id
to magazine_article_map.article_id,
join magazine_article_map.magazine_id
to magazines.magazine_id;
// the next line is the Link Attribute. Note that it also specifies
// the SQL type of INTEGER so that the DDL generator can correctly