Java Reference
In-Depth Information
<many-to-one
name="user"
column="aduser"
class="sample.entity.User"
not-null="true"
foreign-key="fk_advert_user"/>
</class>
</hibernate-mapping>
Once you have created the individual mapping files, you need to tell Hibernate where to
find them. If you're using a Hibernate configuration file, as in the Chapter 1 example, the sim-
plest thing to do is include links to the mapping files directly within this.
For our example, take the configuration file described for Chapter 1 (Listing 1-5) and add
the following three mapping resource entries:
<mapping resource="sample/entity/Advert.hbm.xml"/>
<mapping resource="sample/entity/Category.hbm.xml"/>
<mapping resource="sample/entity/User.hbm.xml"/>
after the following line:
<mapping resource="sample/entity/Message.hbm.xml"/>
This section may seem confusing, as it is something of a flying visit to the subject of map-
pings and some of their whys and wherefores. We provide a more in-depth discussion of
mapping in later chapters—specifically, general mapping concepts in Chapter 5, and XML-
based mapping files in Chapter 7. We also discuss how you can use the new Java 5 Annotations
features to represent mappings directly in your source code in Chapter 6.
Creating the Tables
With the object mapping in place and our Hibernate configuration file set up correctly, we
have everything we need to generate a script to create the database for our application by
invoking the exportDDL task. This builds the entities shown in Figure 3-2.
Even though we can generate the database directly, we also recommend taking some
time to work out what schema you would expect your mappings to generate. This allows
you to “sanity check” the script to make sure it corresponds with your expectations. If you
and the tool both agree on what things should look like, then all is well and good; if not,
your mappings may be wrong or there may be a subtle error in the way that you have
related your data types.
Search WWH ::




Custom Search