Java Reference
In-Depth Information
Mapping Composition
Figure 7-2 shows the class diagram and the entity relationship diagram for a composition rela-
tionship between two classes. Here, the Advert class is composed of a Picture class in addition
to its normal value types.
Figure 7-2. Representing composition
Composition is the strongest form of aggregation—in which the life cycle of each object
is dependent upon the life cycle of the whole. Although Java does not make the distinction
between other types of aggregation and composition, it becomes relevant when we choose to
store the components in the database, because the most efficient and natural way to do this
is to store them in the same table.
In our example, we will look at an Advert class that has this relationship with a Picture
class. The idea is that our advert is always going to be associated with an illustration (see
Listings 7-4 and 7-5). In these circumstances, there is a clear one-to-one relationship that
could be represented between two distinct tables, but which is more efficiently represented
with one.
Listing 7-4. The Class Representing the Illustration
package com.hibernatebook.xmlmapping;
public class Picture {
public Picture(String caption, String filename) {
this.caption = caption;
this.filename = filename;
}
Picture() {
}
public String getCaption() {
return this.caption;
}
Search WWH ::




Custom Search