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

Basic Structure

The basic format of a *.hbm.xml mapping file is shown in Listing 5.1. The first line is a standard XML declaration. The DOCTYPE specifies a DTD (defining the structure of the mapping document). This DTD is included in the main hibernate2.jar file and is also available online. If you are using the standard XML parsing libraries included with the Hibernate distribution, Hibernate will use the DTD included in the hibernate2.jar file, not the online file. Opening the file in a strict XML editor or viewer, however, may result in an error if no Internet connection is available.

Listing 5.1. Basic Mapping File Declaration
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
      "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
      "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
...
</hibernate-mapping>

Depending on your needs, you may wish to replace the PUBLIC DOCTYPE declaration with a SYSTEM declaration pointing to a file on the local file system, as shown in Listing 5.2. This should only be done during development, and should be avoided if possible.

Obviously, the location of the DTD on your local file system may vary.

Listing 5.2. Mapping File with Local DTD Declaration
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping SYSTEM "C:\devenv\hibernate-2.1.2\src\net\sf\hibernate
\hibernate-mapping-2.0.dtd">
<hibernate-mapping>
...
</hibernate-mapping>

The root element is hibernate-mapping. Most often, you will find one or more class declaration elements in a hibernate-mapping, as well as import declarations to bring in other classes, queries, and other information. For the full range of options, see the hibernate-mapping tag below.