Java Reference
In-Depth Information
The name field : Represents the name of the user. It should be stored in a column called
name . It has type String . We do not permit duplicate names to be stored in the table.
The password field : Represents a given user's password. It should be stored in a column
called password . It has type String .
Bearing these features in mind, the mapping file in Listing 3-11 should be extremely easy
to follow.
Listing 3-11. The Mapping of the User Class into the Database
<?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">
<hibernate-mapping>
<class name="com.hibernatebook.chapter3.User" table="aduser">
<id name="id" type="long" column="id">
<generator class="native"/>
</id>
<property name="name" column="name" type="string" unique="true"/>
<property name="password" column="password" type="string"/>
</class>
</hibernate-mapping>
The Category mapping presents another type of relationship: many-to-many. Each
Category object is associated with a set of adverts, while any given advert can be associated
with multiple categories.
The <set> element indicates that the field in question has a java.util.Set type with the
name adverts . This sort of relationship requires the creation of an additional link table, so we
specify the name of the table containing that information.
We state that the primary key (used to retrieve items) for the objects contained in the
link table is represented by the id column, and provide the fully qualified name of the class
type contained in the table. We specify the column in the link table representing the adverts
associated with each category.
Again, this is complicated when described, but if you look at the example table from
Listing 3-14, the need for each field in the mapping becomes clear (see Listing 3-12).
Listing 3-12. The Mapping of the Category Class into the Database
<?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">
Search WWH ::




Custom Search