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

Hibernate's Mapping System

Object/relational mapping refers to the notion of binding an object-oriented programming language to a relational database. In lieu of a theoretical discussion of object/relational mapping, let's take a quick look at how this mapping occurs in Hibernate.

The heart of the object/relational mapping system in Hibernate is an XML file, normally named *.hbm.xml. This mapping file describes how a database schema is bound to a set of Java classes. Hibernate (and related projects) provide tools for generating *.hbm.xml files both from existing database schema and from Java code. Some developers start with the *.hbm.xml file because it may be the most straightforward way to describe an application. Once you have a *.hbm.xml file, you can generate the supporting Java code, the database schema, or both. Regardless of your approach, it's important to understand the importance of *.hbm.xml files when working with Hibernate.

Listing 1.3 shows a simple Hibernate mapping file. A database-oriented developer would interpret this mapping as describing a system with two tables, weblog and post.

Listing 1.3. Example Hibernate Mapping File
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping SYSTEM "http://hibernate.sourceforge.net/hibernate-mapping-2.0
.dtd" >
<hibernate-mapping package="com.cascadetg.ch01">

      <class name="Weblog" table="weblog">
            <id name="id" column="ID" type="long" unsaved-
                 value="null">
              <generator class="native"/>
            </id>
            <property name="title" column="title"
                  type="string" length="50" not-null="true"/>
            <set name="Posts" lazy="true">
                  <key column="weblog_id" />
                  <one-to-many class="Post"/>
            </set>
      </class>

      <class name="Post" table="post">
            <id name="id" column="ID" type="long" unsaved-
                 value="null">
              <generator class="native"/>
            </id>
            <property name="title" column="title" type="string"
                  length="50" not-null="true"/>
            <property name="body" column="body" type="string"
                  length="255" not-null="true"/>
            <property name="postTimestamp" column="posttimestamp"
                  type="timestamp" />
            <many-to-one name="Weblog" class="Weblog"
                  column="weblog_id" not-null="true"/>
      </class>
</hibernate-mapping>

Listing 1.4 shows the resulting MySQL schema, generated automatically by Hibernate using the *.hbm.xml file shown in Listing 1.3. In particular, note the post.weblog_id column generated by Hibernate, used to track the relationship between a post and a weblog.

Listing 1.4. MySQL Schema for Weblog and Post
mysql> desc post;
+---------------+-------------+------+-----+---------+--------+
| Field         | Type        | Null | Key | Default | Extra  |
+---------------+-------------+------+-----+---------+--------+
| ID            | bigint(20)  |      | PRI | NULL    |
                                              auto_increment  |
| title         | varchar(50) |      |     |         |        |
| body          | text        |      |     |         |        |
| posttimestamp | datetime    | YES  |     | NULL    |        |
| weblog_id     | bigint(20)  |      | MUL | 0       |        |
+---------------+-------------+------+-----+---------+--------+

mysql> desc weblog;
+-------+-------------+------+-----+---------+----------------+
| Field | Type        | Null | Key | Default | Extra          |
+-------+-------------+------+-----+---------+----------------+
| ID    | bigint(20)  |      | PRI | NULL    | auto_increment |
| title | varchar(50) |      |     |         |                |
+-------+-------------+------+-----+---------+----------------+

Similarly, a set of Java class files generated by Hibernate from the same mapping file is shown in Figure 1.3. The generated Java objects act as data placeholders, but are essentially ordinary Java objects, conforming to standard JavaBean patterns.

Figure 1.3. Weblog & Post Java Objects


WHAT ARE JAVABEANS?

JavaBeans refer to the standard, base component model for Java applications. Briefly stated, JavaBeans are Java classes that define properties in the form String getProperty() /setProperty(String in). A broad suite of APIs, tools, and software use JavaBeans as a core pattern. You will need to be familiar with the JavaBeans specification to use Hibernate, JSF, EJB 3.0, Swing, and almost any other significant Java library.

For more information on JavaBeans, consult any introductory Java text or http://java.sun.com/j2se/1.5.0/docs/guide/beans/index.html.


After creating this mapping, a Java developer can use a few simple Hibernate methods to insert, delete, update, and select data using these Java objects. This mapping between Java objects and relational tables is much more than a simplistic one-table-is-one-class system. As shown above, there is a relationship between the two tables: a weblog can have multiple posts. A Java developer can retrieve the posts associated with a given weblog merely by issuing a single Weblog.getPosts()call. Hibernate will automatically load the related posts from the database, bundle them into a set, and then return the data as Java objects.