Java Reference
In-Depth Information
Table 5-12. A Many-to-Many User / Email Link Table
ID
UserID
EmailID
1
1
1
2
1
2
3
1
3
4
1
4
5
2
1
6
2
2
Table 5-12 might describe a situation in which the user dcminter receives all e-mail sent
to any of the four addresses, whereas jlinwood receives only e-mail sent to his own accounts.
When the link table has its own independent primary key, as with the association shown
in Figure 5-8, thought should be given to the possibility that a new class should be created to
represent the contents of the link table as an entity in its own right.
Figure 5-8. A many-to-many relationship represented by a link table (duplicates are permitted
because of the use of a surrogate key)
Applying Mappings to Associations
The mappings are applied to express the various different ways of forming associations in the
underlying tables—there is no automatically correct way to represent them.
In addition to the basic choice of the approach to take, the mappings are used to specify
the minutiae of the tables' representations. While Hibernate tends to use sensible default val-
ues when possible, it is often desirable to override these. For example, the foreign key names
generated automatically by Hibernate will be effectively random—whereas an informed
developer can apply a name (e.g., FK_USER_EMAIL_LINK ) to aid in the debugging of constraint
violations at run time.
Types of Mapping
At present, Hibernate supports two standard ways to express the mappings.
The technique that has been available the longest is the use of XML mapping files. As the
most mature approach, this is currently the best way to control Hibernate, and gives the most
sophisticated control over the Hibernate feature set. You have seen examples of simple map-
ping files in Chapters 1 and 3.
Search WWH ::




Custom Search