User's Manual

Chapter 9. Persistence Tutorial 73
// create the mapping table with the page_number column.
BigDecimal pageNumber = magazine_article_map.page_number INTEGER;
}
For more information about Link Attributes and their use, see Section 9.8 Link Attributes.
9.3.5. Using Java to Access Associations
Now that you have seen how to declare associations within PDL, you can learn the different ways to
access the information from Java. In Java, Associations are accessed with two classes: DataAssoci-
ation, similar to a Java Collection, and DataAssociationCursor, similar to a Java Iterator.
9.3.5.1. Using Standard Associations
public Collection getArticles(DataObject obj) {
LinkedList articles = new LinkedList();
DataAssociationCursor cursor =
((DataAssociation) obj.get("articles")).cursor();
while (cursor.next()) {
articles.add(cursor.getDataObject());
}
return articles;
}
The next example shows how to associate one item with another. In this case, you are associating an
Article with a Magazine by adding the article to the "articles" association. By calling save(), you
are signalling for the data object to fire the appropriate insert and update association events (remove
the "\" and make it all one line).
public void addArticle(DataObject magazine, DataObject article) {
DataAssociation association = (DataAssociation) magazine.get\
("articles");
association.add(article);
magazine.save();
}
Note
There are two important things to realize when dealing with adding items to Associations and
iterating through them:
Unlike DataCollection, DataAssociation has been separated into two distinct entities. If you
want to loop through the items in the association, or filter or order the association, use a DataAs-
sociationCursor. If you want to add or remove items from the association, use the DataAsso-
ciation object. The DataAssociation is a property of the DataObject and is shared by all code
accessing the data object. The DataAssociationCursor is essentially a local copy of the associ-
ation that can be filtered, ordered, and iteratred through without any external consequences.
While this next item is actually a feature of Domain Objects, it is important to mention here as
well. When adding items to associations using the DomainObject (and therefore any ACSObject),
there are three choices of which method to use. If the association has an association of 0..n
(or any upper bound
1), developers should use the add(String propertyName, DataObject
dataObject) method or the add(String propertyName, DomainObject dobj) method. If the