Datasheet
Our CD class can be further complicated by adding an attribute based on another class type. For
example:
public class CD implements Serializable {
String title;
String artist;
ArrayList tracks;
public CD(String title, String artist) {
this.title = title;
this.artist = artist;
tracks = new ArrayList();
}
public void addTrack(Track track) {
tracks.add(track);
}
private class Track {
String name;
int length;
public track(String name, int length) {
this.name = name;
this.length = length;
}
}
}
We’ve added another class called Track, which is added to the track ArrayList instead of a single string.
With the Track class added to the class model, we have a new situation that needs to be handled through
an appropriate mapping. The most obvious choice is to add another database table for mapping between
the class and the permanent storage. For example:
create table tracks (
ID int not null primary key auto_increment,
name varchar(256),
length int
);
The new database table looks similar to the CD_tracks database created in the previous example but
includes a little more information. (We could have used the CD_tracks schema and added the length
column.)
After these examples, it should be clear that saving a set of objects to permanent storage isn’t a simple
task. In order to correctly put an object’s attributes in a database, you must have a clear plan in place
with the proper databases and mappings. As you can see, though, once a class has been defined, the
database tables and mappings become clear. Thus, in most design situations, the database modeling
should occur after the classes have been defined.
7
Introduction to Mapping Objects to Relational Databases
03_576771_c01.qxd 9/1/04 12:09 PM Page 7










