Database Relationships
There are a few basic mechanisms for tracking relationships between tables in a database. The many-to-one and one-to-many relationships are used to model the simplest versions of a foreign key, and should be considered your default (especially if you are modeling legacy data). The next stage, using Hibernate to manage collection tables using the many-to-many relationship, should be considered a somewhat more advanced technique (but not necessarilydon't fall into the trap of trying to jam an incompatible model into a many-to-many relationship). Finally, a one-to-one model can be useful in certain situations.
Many-to-One
The name many-to-one simply refers to another object (in Java terms) or a foreign key (in database terms).
The many-to-one and one-to-many terms describe two sides of the same relationship. In Figure 7.1, the many-to-one relationship is used to describe the relationship from the perspective of the Parts table.

One-to-Many
The one-to-many relationship is used to model a collection (in Java terms) or the referred table of a foreign-key relationship (in database terms). The precise behavior of a one-to-many collection is determined by the type of collection (as described below, under the heading Java Collection Relationships).
A one-to-many relationship is generally modeled by a table with a foreign key. In Figure 7.1, for instance, a one-to-many relationship is described by the CarModel table. The one-to-many model is used to represent the relationship from the perspective of the target table.
This relationship is modeled in the CarModel mapping file, but note that the CarModel table does not actually contain any data referring to the Parts table. The only notion of the relationship that is expressed in the database comes from the addition of a foreign key (as shown in Chapter 4). Putting this in parent/child terms, the relationship is modeled on the parent, but technically the data is stored in the child.
If the one-to-many relationship is part of a bi-directional relationship (in other words, if you can navigate using your Java code from the CarModel object to Parts and back), you will probably wish to set the inverse="true" attribute for the collection tag used to wrap the relationship. For more information on bi-directional relationships, see the section at the end of this chapter.
Many-to-Many
A many-to-many relationship is used to model a collection (in Java terms) or a collection table (in database terms). In a many-to-many relationship, neither the parent nor the child table contains data about the relationship. Instead this information is stored in a collection table. Figure 7.2 shows a collection table, CarModelOrders.

Because there is no additional information in the CarModelOrders collection table, using a many-to-many relationship allows Hibernate to effectively "hide" the CarModelOrders table. Since CarModelOrders is present in the mapping files for CarModels and Orders, there is no need to make a separate mapping file for it.
Hibernate expects a many-to-many relationship to be mapped as a simple collection table, meaning that no additional data is stored in the table. You may occasionally wish to store a many-to-many mapping that also contains additional information. Since this table would no longer be a pure binding, it should be modeled with a new class and many-to-one/one-to-many relationships, as shown in Chapter 4.
If the many-to-many relationship is part of a bi-directional relationship (i.e., you can expect to navigate using your Java code from CarModel to Orders and back), you must set the inverse="true" attribute on one side of the relationship. For more information on bi-directional relationships, see the section at the end of this chapter.
One-to-One
A one-to-one relationship models two tables that are logically "glued" together in some fashion. For example, an existing human resources database might have a table used to represent employees and another table to represent employee medical records. The table for employees is available to all human resources staff, whereas the medical record table is only accessible via a special administrative connection to maintain high security. You would therefore use one-to-one mapping in the administrative application to manage this relationship.
You will need to use the assigned generator for one side of your one-to-one relationship. See Chapter 6 for more information.
|