Generate the Java Class

The mapping document contains information about both the database structure used to store information and the Java class that represents it at runtime. Let's look at the class first.

The Hibernate extensions you installed earlier in this PDF 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 3.

Example 3. The Ant build file updated for code generation

<?xml version="1.0"?> <project name="Getting Started with Hibernate 3" default="db" basedir="."> <!-- Set up properties containing important project directories --> <property name="source.root" value="src"/> <property name="class.root" value="classes"/> <property name="lib.dir" value="lib"/> <property name="data.dir" value="data"/> <!-- Set up the class path for compilation and execution --> <path id="project.class.path"> <!-- Include our own classes, of course --> <pathelement location="${class.root}" /> <!-- Include jars in the project library directory --> <fileset dir="${lib.dir}"> <include name="*.jar"/> </fileset> </path> <target name="db" description="Runs HSQLDB database management UI against the database file--use when application is not running"> <java classname="org.hsqldb.util.DatabaseManager" fork="yes"> <classpath refid="project.class.path"/> <arg value="-driver"/> <arg value="org.hsqldb.jdbcDriver"/> <arg value="-url"/> <arg ...

Get Getting Started with Hibernate 3 now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.