Java Reference
In-Depth Information
Mapping Other Associations
In Figure 7-3, the Advert class includes an instance of a Picture class. The relationship in the
tables is represented with the Picture table having a foreign key onto the Advert table.
Figure 7-3. Mapping an aggregation or composition relationship
A one-to-one correspondence does not absolutely require you to incorporate both parties
into the same table. There are often good reasons not to. For instance, in the Picture example,
it is entirely possible that while the initial implementation will permit only one Picture per
Advert , a future implementation will relax this relationship. Consider this scenario from the
perspective of the database for a moment (see Table 7-17).
Table 7-17. The Advert Table
ID
Title
Contents
PictureCaption
PictureFilename
advert001.jpg
1
Bike
Bicycle for sale
My bike (you can ride it if you like)
advert002.jpg
2
Sofa
Sofa, comfy but used
Chesterfield sofa
advert003.jpg
3
Car
Shabby MGF for sale
MGF VVC (BRG)
If we want to allow the advert for the sofa to include another picture, we would have to
duplicate some of the data, or include null columns. It would probably be preferable to set up
a pair of tables: one to represent the adverts, and one to represent the distinct tables (as
shown in Tables 7-18 and 7-19).
Table 7-18. The Refined Advert Table
ID
Title
Contents
1
Bike
Bicycle for sale
2
Sofa
Sofa, comfy but used
3
Car
Shabby MGF for sale
Search WWH ::




Custom Search