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