Write a Mapping Document

Hibernate uses an XML document to track the mapping between Java classes and relational database tables. This mapping document is designed to be readable and hand-editable. You can also start by using graphical CASE tools (like Together, Rose, or Poseidon) to build UML diagrams representing your data model and feed these into AndroMDA, turning them into Hibernate mappings. The Hibernate Tools package you downloaded earlier also gives you several of these capabilities if you are working with it in Eclipse.

Here, we’ll write an XML document by hand, showing it’s quite practical. Let's start by creating a mapping for tracks, pieces of music that can be listened to individually, or as part of an album or playlist. To begin with, we’ll keep track of the track’s title, the path to the file containing the actual music, its playing time, the date on which it was added to the database, and the volume at which it should be played (in case the default volume isn’t appropriate because it was recorded at a very different level than other music in the database).

Fire up your favorite text editor, and create the file Track.hbm.xml in the src/com/oreilly/hh directory you created in “Set Up a Project Hierarchy”. (If you skipped that section, you’ll need to go back and follow it, because this example relies on the project structure and tools we set up there.) Type in the mapping document shown in Example 2.


You may be thinking there’s a lot of dense information in this file. ...

Get Getting Started with Hibernate 3 now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.