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

Initial Schema

In this scenario, development is based on an existing schema. Application designers are often not allowed to significantly update or change a schema, perhaps because it is in use by other applications, or because the database is a legacy system, or for any number of other possible reasons. The point is that the database design in this scenario drives the application development. A schema is given, and may occasionally be embroidered (for example, adding a column), but the overall structure is fixed prior to application development.

Listing 4.1 shows the script used to generate the schema for the database in this chapter. Note that this script is written to automatically drop and recreate the tables used in the chapter each time it is run, destroying any data present in the tables (obviously this would not be used in a "real" system).

The schema in this example includes several MySQL specific commands. For example, the TYPE=InnoDB command tells MySQL to convert the tables to the InnoDB format, a more modern format for storing database data than the default MySQL format. Similarly, the foreign-key checks are disabled to avoid errors when dropping the tables. The schema described in this chapter is based on MySQL 4.0.18. Depending on the database you are using, you may need to modify this schema script to be able to execute it. Alternatively, you may wish to use the graphical administrative tool included with your database (if any) to create this schema.

PORTABLE SCHEMA SCRIPTS

If you are used to another database, the MySQL schema commands below may look unfamiliar. Worse, unless you are running MySQL, you'll almost certainly have to edit the script for your own database.

This is yet another reason why I strongly prefer working from a mapping file if possible. Hibernate includes schema management functionality (as described in Chapter 11). This allows you to define a common schema structure in your mapping file and rely on Hibernate to generate dialect-specific scripts for a suite of databases.


Pay close attention to the commands to create indexes and foreign key constraints at the end of the script. Middlegen (the tool used later in this chapter) reads these constraints and uses them to generate relationship mappings. In other words, Middlegen reads these key relationships and uses them to establish relationships in the generated Hibernate mapping files (and from there in the generated Java source).

If you wish to see a graphical representation of the tables generated by this script, you may wish to skip ahead to glance at the tables in Figure 4.2.

Listing 4.1. Schema Generation Script
USE hibernate;

SET FOREIGN_KEY_CHECKS=0;
DROP TABLE IF EXISTS Course;
DROP TABLE IF EXISTS Student;
DROP TABLE IF EXISTS Exam;
DROP TABLE IF EXISTS ExamResult;
SET FOREIGN_KEY_CHECKS=1;
CREATE TABLE Course
(
      ID BIGINT UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY
);
ALTER TABLE Course ADD title CHAR(100);
ALTER TABLE Course ADD quarter CHAR(4);

CREATE TABLE Student
(
      ID BIGINT UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY
);
ALTER TABLE Student ADD firstName CHAR(100);
ALTER TABLE Student ADD lastName CHAR(100);
ALTER TABLE Student ADD idString CHAR(20);

CREATE TABLE Exam
(
      ID BIGINT UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY
);
ALTER TABLE Exam ADD courseID BIGINT UNSIGNED;
ALTER TABLE Exam ADD date TIMESTAMP;
ALTER TABLE Exam ADD comment CHAR(255);

CREATE TABLE ExamResult
(
      ID BIGINT UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY
);
ALTER TABLE ExamResult ADD score INT;
ALTER TABLE ExamResult ADD studentID BIGINT UNSIGNED;
ALTER TABLE ExamResult ADD examID BIGINT UNSIGNED;


ALTER TABLE Course TYPE=InnoDB;
ALTER TABLE Student TYPE=InnoDB;
ALTER TABLE Exam TYPE=InnoDB;
ALTER TABLE ExamResult TYPE=InnoDB;

ALTER TABLE Exam ADD INDEX (courseID);
ALTER TABLE ExamResult ADD INDEX (studentID);
ALTER TABLE ExamResult ADD INDEX (examID);

ALTER TABLE Exam ADD CONSTRAINT FK_exam_courseID FOREIGN KEY (courseID) REFERENCES Course 
(ID);
ALTER TABLE ExamResult ADD CONSTRAINT FK_examresult_studentID FOREIGN KEY (studentID)
 REFERENCES Student (ID);
ALTER TABLE ExamResult ADD CONSTRAINT FK_examresult_examID FOREIGN KEY (examID) REFERENCES
 Exam (ID);

SHOW TABLES;

Figure 4.2. Middlegen Graphical Interface