2.2. Generating Some Class

Our mapping contains information about both the database and the Java class between which it maps. We can use it to help us create both. Let's look at the class first.

2.2.1. How do I do that?

The Hibernate Extensions you installed in Chapter 1 included a tool that can write Java source matching the specifications in a mapping document, and an Ant task that makes it easy to invoke from within an Ant build file. Edit build.xml to add the portions shown in bold in Example 2-2.

Example 2-2. The Ant build file updated for code generation
 1  <project name="Harnessing Hibernate: The Developer's Notebook"	
 2           default="db" basedir=".">
 3    <!-- Set up properties containing important project directories -->
 4    <property name="source.root" value="src"/>
 5    <property name="class.root" value="classes"/>
 6    <property name="lib.dir" value="lib"/>
 7    <property name="data.dir" value="data"/>
 8
 9    <!-- Set up the class path for compilation and execution -->
10    <path id="project.class.path">
11        <!-- Include our own classes, of course -->
12        <pathelement location="${class.root}" />
13        <!-- Include jars in the project library directory -->
14        <fileset dir="${lib.dir}">
15          <include name="*.jar"/>
16        </fileset>
17    </path>
18
19    <target name="db" description="Runs HSQLDB database management UI
20  against the database file--use when application is not running">
21        <java classname="org.hsqldb.util.DatabaseManager" 22 fork="yes"> 23 <classpath refid="project.class.path"/> 24 <arg value="-driver"/> ...

Get Hibernate: A Developer's Notebook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.