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

Generated Schema

The application in this chapter is intended to generate the database schema from the mapping files, generated in turn from the Java source. The representation of the Artifact object in a database is fairly straightforward, as shown in Listing 3.9.

Listing 3.9. Artifact Table Definition
mysql> desc artifact;
+----------------+--------------+------+-----+---------+------+
| Field          | Type         | Null | Key | Default | Extra
|
+----------------+--------------+------+-----+---------+------+
| id             | bigint(20)   |      | PRI | NULL    |
                                               auto_increment |
| comments       | varchar(255) | YES  |     | NULL    |      |
| dateCreated    | datetime     | YES  |     | NULL    |      |
| dateDiscovered | datetime     | YES  |     | NULL    |      |
| description    | varchar(255) | YES  |     | NULL    |      |
| location       | varchar(255) | YES  |     | NULL    |      |
| title          | varchar(255) | YES  |     | NULL    |      |
+----------------+--------------+------+-----+---------+------+
7 rows in set (0.00 sec)

The representation of the Owner class and the related subclasses, however, is a bit more complex, as shown in Listing 3.10.

Listing 3.10. Owner Table Definition
mysql> desc owner;
+---------------+--------------+------+-----+---------+-------+
| Field         | Type         | Null | Key | Default | Extra
+---------------+--------------+------+-----+---------+-------+
| id            | bigint(20)   |      | PRI | NULL    | auto_                             
                        increment |
| discriminator | varchar(255) |      |     |         |        |
| name          | varchar(255) | YES  |     | NULL    |        |
| dateCreated   | datetime     | YES  |     | NULL    |        |
| location      | varchar(255) | YES  |     | NULL    |        |
| owner         | bigint(20)   | YES  | MUL | NULL    |        |
| endDate       | datetime     | YES  |     | NULL    |        |
| startDate     | datetime     | YES  |     | NULL    |        |
| dateOpened    | datetime     | YES  |     | NULL    |        |
| birthDate     | datetime     | YES  |     | NULL    |        |
| deathDate     | datetime     | YES  |     | NULL    |        |
+---------------+--------------+------+-----+---------+-------+

Looking at the owner table in Listing 3.10 more closely, several interesting details emerge. The discriminator column is used to store the class type information regarding the record. Listing 3.11 shows an example of several records stored in the owner table; note the class information.

Listing 3.11. Sample Owner Records
mysql> select id, discriminator, name from owner;
+----+-------------------------------+-------------------------+
| id | discriminator                 | name                    |
+----+-------------------------------+-------------------------+
|  1 | com.cascadetg.ch03.Person     | Smith, Bob              |
|  2 | com.cascadetg.ch03.Foundation | Smith Foundation        |
|  3 | com.cascadetg.ch03.Museum     | Waldendorf              |
|  4 | com.cascadetg.ch03.Exhibition | New Acquisitions 2005   |
+----+-------------------------------+-------------------------+
4 rows in set (0.00 sec)

Any column not relevant to a particular subclass is set to null. For example, a Person has no date-opened property, and therefore the dateOpened column for Person records is set to null.

Hibernate automatically manages the discriminator for you as you work with persistent objects. For example, when Hibernate retrieves the four records shown in Listing 3.11, it will automatically instantiate the proper object and set the values for you, returning a set of Owner objects with the proper underlying class.

In addition to the expected two tables, Hibernate also uses a third table to manage the relationships between artifacts and their owners, as shown in Listing 3.12. This table isn't mapped to a particular Java class; it is instead implied by the bi-directional set references on both Owner and Artifact. In database terminology, this is a many-to-many relationship. For more information on the link between a set collection and a many-to-many database relationship, see Chapter 7.

Listing 3.12. Ownership Table
mysql> desc ownership;
+-------------+------------+------+-----+---------+------------+
| Field       | Type       | Null | Key | Default | Extra      |
+-------------+------------+------+-----+---------+------------+
| owner_id    | bigint(20) |      | PRI | 0       |            |
| artifact_id | bigint(20) |      | PRI | 0       |            |
+-------------+------------+------+-----+---------+------------+
2 rows in set (0.00 sec)