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.

|
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.
|