Java Reference
In-Depth Information
Listing 7-6.
The New Picture Mapping
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<class name="com.hibernatebook.xmlmapping.Picture">
<id name="id" type="int">
<generator class="native"/>
</id>
<many-to-one
name="advert"
class="com.hibernatebook.xmlmapping.Advert"
column="advert"/>
<property name="caption" type="string" length="255"/>
<property name="filename" type="string" length="32"/>
</class>
If you still object to the many-to-one relationship, you will probably find it cathartic to
note that we have explicitly constrained this relationship with the
unique
attribute. You will
also find it reassuring that in order to make navigation possible directly from the
Advert
to its
associated
Picture
, we can in fact use a one-to-one mapping entry. We need to be able to nav-
igate in this direction because we expect to retrieve adverts from the database, and then
display their associated pictures (see Listing 7-7).
Listing 7-7.
The Revised
Advert
Mapping
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<class name="com.hibernatebook.xmlmapping.Advert">
<id name="id" type="int">
<generator class="native"/>
</id>
<property name="title" type="string" length="255"/>
<property name="content" type="text"/>
<one-to-one name="picture"
class="com.hibernatebook.xmlmapping.Picture"
property-ref="picture">
</class>
Now that we have seen how one-to-one and many-to-one relationships are expressed, we
will see how a many-to-many relationship can be expressed.
Search WWH ::
Custom Search