User's Manual

70 Chapter 9. Persistence Tutorial
// a "reference key" declaration and "id" is not defined
// as one of the attributes
reference key (magazines.magazine_id);
}
9.3. Associations
So far, the documentation has discussed how to create simple Data Objects to access the database.
These features, while sufficient to build many systems, lack the ability to relate object types to each
other. To address these needs, the persistence system contains the concept of associations.
This document discusses how the persistence layer allows developers to associate Data Objects and
how these associations can be saved in the database without needing to involve the Java code in how
the associations are actually stored. More concretely, the document first discusses how an association
is structured within PDL. It then defines the PDL for Articles, Paragraphs, and Authors and
show hows to relate them through Two-Way Associations, Composite Associations, and One Way
Associations.
9.3.1. Association Blocks
Most commonly you associate objects to each other when both objects need to know to which objects
they are associated. Magazines and Articles, Users and Groups, and Employees and Offices are all
examples of this type of association. In the PDL below, an Article Object Type and an "association
block" are defined to associate the Articles to Magazines. Association Block definitions are simi-
lar to Object Type definitions in that they both have attribute definitions. Instead of the Attribute being
set as equal to a single column within the database, the Attribute is set as equal to a Join Path.
object type Article {
BigDecimal id = articles.title;
String title = articles.title;
object key (articles.article_id);
}
// this is an "association block" associating "articles" and "magazines"
association {
// note that the Attribute Type is an Object Type (Article)
// and not a standard Java Type. Also notice the order of the
// join path and see the note below.
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;
}