User's Manual

Chapter 9. Persistence Tutorial 71
Note
The order of the join path is important. The information that the developer has must come first.
That is, when the developer needs to retreive articles, the information he has is the Magazine
(he needs to know for which magazine to get the articles). Therefore, the first line of the join path
specifies how to join the magzines table to the mapping table. From then on, it should be in order so
that the last section of the join element uses the same table as the first section of the next join
element.
9.3.2. Composite Associations
Composite relationships are a special type of Association. Composite relationships are useful for mod-
eling relationships between objects where a contained object cannot exist outside its container object.
The main difference between a Composition and a standard Association is that within a Composition,
one of the objects cannot exist without the other.
In the following example, a Paragraph is a component of the Article (they therefore have a com-
posite relationship). That is, it does not make sense for a Paragraph to exist outside of an article. There
are many different ways to signify a relationship as composite but the easiest way is to add the com-
ponent keyword before the component Object Type (in this case, the paragraph is a component of
the article)
object type Paragraph {
BigDecimal id = paragraphs.paragraph_id INTEGER;
String text = paragraphs.text VARCHAR(4000);
object key (paragraphs.paragraph_id);
}
association {
Article[1..1] articles = join paragraphs.article_id
to articles.article_id;
// notice the component keyword indicates that if the article does
// not exist then the paragraph also does not exist
component Paragraph[0..n] paragraphs = join articles.article_id
to paragraphs.article_id;
}
Another way to make the same association is to use the composite keyword on the role that points
toward the composite end of an association in order to indicate that the association is a composition.
For example:
association {
composite Article[1..1] articles = join paragraphs.article_id
to articles.article_id;
Paragraph[0..n] paragraphs = join articles.article_id
to paragraphs.article_id;
}
The final way to signify the relationship is to use keywords for both object types. This displays the
same behavior as the two examples above but it also valid.
association {
composite Article[1..1] articles = join paragraphs.article_id
to articles.article_id;
component Paragraph[0..n] paragraphs = join articles.article_id
to paragraphs.article_id;