More Books
Hibernate: A J2EE Developer's Guide
Hibernate: A J2EE™ Developer's Guide
Table of Contents
Copyright
Acknowledgments
About the Author
Preface
Required Skills
Roadmap
Chapter 1. Overview
Why Object/Relational Mapping?
What Is Hibernate?
Comparing JDBC to Hibernate
Hibernate's Mapping System
Other Java/Database Integration Solutions
How to Obtain and Install
Supported Databases
Chapter 2. Getting Oriented
Application Architecture
Mapping Files
Generating Java Source
Application Configuration
Web Application
JSP Interface
Chapter 3. Starting from Java
Java Object Model
Generated Mapping Files
Generated Schema
Working with Artifacts and Owners
Chapter 4. Starting from an Existing Schema
Initial Schema
Using Middlegen
Generated Mapping Files
Generated Java
Working with the Database
Chapter 5. Mapping Files
Basic Structure
Mapping File Reference
Chapter 6. Persistent Objects
Sessions
Objects and Identity
Life-Cycle Methods
Chapter 7. Relationships
Database Relationships
Java Collection Relationships
Java Class Relationships
Any-Based Relationships
Bi-directional Relationships
Chapter 8. Queries
HQL
HQL Reference
Select
From
Where
Group By
Having
Order By
Criteria Queries
Native SQL Queries
Chapter 9. Transactions
Introduction to Transactions
Optimistic and Pessimistic Locking
Chapter 10. Performance
Finding and Solving Problems
Queries
Inserts
Connection Pooling
Caching
Chapter 11. Schema Management
Updating an Existing Schema
Generating Update and Drop Scripts
Chapter 12. Best Practices, Style Guide, Tips and Tricks
Reducing Code with Inversion of Control
Reducing Session Creation Impact with ThreadLocal
Using Hibernate as an EJB BMP Solution
Integrating with Other Technologies
Applications That Use Hibernate
Strategies for Getting Started
Chapter 13. Future Directions
Hibernate 3.0
EJB 3.0
Here and Now
Index
SYMBOL
A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
Q
R
S
T
U
V
W
X

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.

Figure 7.1. Basic Foreign-Key Relationship


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.

Figure 7.2. Collection Table


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.