Datasheet
ArrayList tracks;
public CD(String title, String artist) {
this.title = title;
this.artist = artist;
tracks = new ArrayListO;
}
public void addTrack(String track) {
tracks. add(track);
}
}
In this new CD class, we’ve added an ArrayList to hold the name of each title on the CD. As you might
expect, this additional attribute introduces a hidden complexity to the mapping between the objects
instantiated from the CD class and the permanent storage: The ArrayList attribute can contain no values
or hundreds of values. There might be 8 to 10 tracks on a CD; an MP3 CD could include hundreds of
songs. In either case, we need to be able to map the ArrayList attribute into a database structure so it can
be permanently recorded when the entire CD is committed.
A common solution is to create a second database table just for the attribute. For example, we might cre-
ate a table like this:
create table CD_tracks (
ID int not null primary key.
track varchar(256)
);
Using the Rush CD object created earlier, we can make a couple of calls to the addTrack() method and
fill the tracks ArrayList:
rush.addTrack("Distant Early Warning");
rush.addTrack("Afterimage");
rush.addTrack("Red Sector A");
When the CD object is saved to the database, information is placed in two different tables: a CD table
+----+--------------------------+-------+
| ID | Title | Artist|
+----+--------------------------+-------+
| 1 | Grace Under Pressure | Rush |
+----+--------------------------+-------+
and a CD_tracks table
+----+---------------------------+
| ID | Track |
+----+---------------------------+
| 1 | Distant Early Warning |
+----+---------------------------+
| 2 | Afterimage |
+----+---------------------------+
| 3 | Red Sector A |
+----+---------------------------+
5
Introduction to Mapping Objects to Relational Databases
03_576771_c01.qxd 9/1/04 12:09 PM Page 5










