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 Mapping Files

The XDoclet tags generate mapping files for you automatically. Despite our five classes, XDoclet only generates two mapping documents and stores the subclasses in the same document as the base class. Therefore, the application only has an Owner.hbm.xml file and an Artifact.hbm.xml.

Listing 3.7 shows the Artifact mapping file, with some of the extraneous white space removed. For additional details on the various attributes and elements, see Chapter 5.

Listing 3.7. Artifact Mapping File
<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>
    <class name="com.cascadetg.ch03.Artifact"
        dynamic-update="false"
        dynamic-insert="false">

        <id name="id" column="id" type="long">
            <generator class="native" />
        </id>

        <property name="comments" type="java.lang.String"
            update="true" insert="true" column="comments"/>

        <property name="dateCreated" type="java.util.Date"
            update="true" insert="true" column="dateCreated" />

        <property name="dateDiscovered" type="java.util.Date"
            update="true" insert="true" column="dateDiscovered"
            />

        <property name="description" type="java.lang.String"
            update="true" insert="true" column="description" />

        <property name="location" type="java.lang.String"
            update="true" insert="true" column="location" />

        <set name="owners" table="ownership" lazy="false"
            inverse="true" cascade="none" sort="unsorted">
              <key column="artifact_id" />
              <many-to-many class="com.cascadetg.ch03.Owner"
                  column="owner_id" outer-join="auto" />
        </set>

        <property name="title" type="java.lang.String"
            update="true" insert="true" column="title" />

        <!--
        To add non XDoclet property mappings, create a
        file named hibernate-properties-Artifact.xml
        containing the additional properties and place
        it in your merge dir.
        -->
    </class>
</hibernate-mapping>

The mapping file for Owner and the subclasses, as shown in Listing 3.8, is significantly more complex than the one for Artifact. In addition to the properties of the Owner class, each subclass is listed with the properties unique to that class. You are more or less allowed to build hierarchies of arbitrary complexitysubclasses of subclasses are allowed, for example. As may be imagined, you can build very complex hierarchies.

Listing 3.8. Owner (and Subclasses) Mapping File
<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-
    2.0.dtd">

<hibernate-mapping>
    <class name="com.cascadetg.ch03.Owner"
        dynamic-update="false" dynamic-insert="false" >

        <id name="id" column="id" type="long">
            <generator class="native" />
        </id>
        <discriminator column="discriminator" type="string" />

        <property name="name" type="java.lang.String"
            update="true" insert="true" column="name" />

        <set name="artifacts" table="ownership" lazy="true"
            inverse="false" cascade="none" sort="unsorted">
              <key column="owner_id" />

              <many-to-many class="com.cascadetg.ch03.Artifact"
                  column="artifact_id" outer-join="auto" />
        </set>

        <!--
            To add non XDoclet property mappings, create a file
            named
            hibernate-properties-Owner.xml
            containing the additional properties
            and place it in your merge dir.
        -->
        <subclass name="com.cascadetg.ch03.Foundation"
            dynamic-update="false" dynamic-insert="false" >

        <property name="dateCreated" type="java.util.Date"
            update="true" insert="true" column="dateCreated" />

        <property name="location" type="java.lang.String"
            update="true" insert="true" column="location" />

        <many-to-one name="owner"
        class="com.cascadetg.ch03.Owner"
            cascade="none" outer-join="auto" update="true"
            insert="true" column="owner" />
          <!--
                  To add non XDoclet property mappings,
                  create a file named
                  hibernate-properties-Foundation.xml
                  containing the additional properties and
                  place it in your merge dir.
          -->
        </subclass>
        <subclass name="com.cascadetg.ch03.Exhibition"
            dynamic-update="false" dynamic-insert="false" >

        <property name="endDate" type="java.util.Date"
            update="true" insert="true" column="endDate" />

        <property name="location" type="java.lang.String"
            update="true" insert="true" column="location" />

        <many-to-one name="owner"
        class="com.cascadetg.ch03.Owner"
            cascade="none" outer-join="auto" update="true"
            insert="true" column="owner" />

        <property name="startDate" type="java.util.Date"
            update="true" insert="true" column="startDate" />
        <!--
            To add non XDoclet property mappings, create a file
            named hibernate-properties-Exhibition.xml
            containing the additional properties and
            place it in your merge dir.
        -->
        </subclass>
        <subclass name="com.cascadetg.ch03.Museum"
            dynamic-update="false" dynamic-insert="false" >

        <property name="dateOpened" type="java.util.Date"
            update="true" insert="true" column="dateOpened" />

        <property name="location" type="java.lang.String"
            update="true" insert="true" column="location" />
        <!--
            To add non XDoclet property mappings, create a file
            named hibernate-properties-Museum.xml
            containing the additional properties and
            place it in your merge dir.
        -->
        </subclass>
        <subclass name="com.cascadetg.ch03.Person"
            dynamic-update="false" dynamic-insert="false" >

        <property name="birthDate" type="java.util.Date"
            update="true" insert="true" column="birthDate" />

        <property name="deathDate" type="java.util.Date"
            update="true" insert="true" column="deathDate" />
            <!--
            To add non XDoclet property mappings, create a file
            named hibernate-properties-Person.xml
            containing the additional properties and
            place it in your merge dir.
        -->
        </subclass>
    </class>
</hibernate-mapping>