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