What just happened?
We took the abstract description of the information about music
tracks that we wanted to represent in our Java code and database, and
turned it into a rigorous specification in the format that Hibernate
can read. Hopefully you'll agree that
it's a pretty compact and readable representation of
the information. Next we'll look at what Hibernate
can actually do with it.
What about...
. . . Other data types, including nested classes and enumerations?
Relationships between tables? Indices? Class hierarchies and
polymorphism? Tables that contain rows we need to ignore? Hibernate
can handle all these things, and we'll cover most of
them in later examples. Appendix A lists the
basic types that Hibernate supports "out of the
box."
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.
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
3 default="db" basedir=".">
4 <!-- Set up properties containing important project directories -->
5 <property name="source.root" value="src"/>
6 <property name="class.root" value="classes"/>
7 <property name="lib.dir" value="lib"/>
8 <property name="data.dir" value="data"/>
9
10 <!-- Set up the class path for compilation and execution -->
11 <path id="project.class.path">
12 <!-- Include our own classes, of course -->
13 <pathelement location="${class.root}" />
14 <!-- Include jars in the project library directory -->
15 <fileset dir="${lib.dir}">
16 <include name="*.jar"/>
17 </fileset>
18 </path>
19
20 <target name="db" description="Runs HSQLDB database management UI
21 against the database file--use when application is not running">
22 <java classname="org.hsqldb.util.DatabaseManager"
23 fork="yes">
24 <classpath refid="project.class.path"/>
25 <arg value="-driver"/>
26 <arg value="org.hsqldb.jdbcDriver"/>
27 <arg value="-url"/>
28 <arg value="jdbc:hsqldb:${data.dir}/music"/>
29 <arg value="-user"/>
30 <arg value="sa"/>
31 </java>
32 </target>
33
34 <! Teach Ant how to use Hibernate's code generation tool >
35 <taskdef name="hbm2java"
36 classname="net.sf.hibernate.tool.hbm2java.Hbm2JavaTask"
37 classpathref="project.class.path"/>
38
39 <! Generate the java code for all mapping files in our source tree >
40 <target name="codegen"
41 description="Generate Java source from the O/R mapping files">
42 <hbm2java output="${source.root}">
43 <fileset dir="${source.root}">
44 <include name="**/*.hbm.xml"/>
45 </fileset>
46 </hbm2java>
47 </target>
48
49 </project>
We added a taskdef (task definition) and a new
target to the build file. The task definition at line 33 teaches Ant a new trick: it tells Ant how to
use the hbm2java tool that is part of the
Hibernate Extensions, with the help of a class provided for this
purpose. Note that it also specifies the class path to be used when
invoking this tool, using the project.class.path
definition found earlier in the file.
New on the Java Boutique:
New Review:
Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling
API boasts simplicity, ease-of-integration, a well-rounded feature
set, and it's free!
New Applet:
Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA
sequences into three useful formats.
Elsewhere on internet.com:
WebDeveloper Java
Lots of Java information on webdeveloper.com
WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.
ScriptSearch Java
Hundreds of free Java code files to download.
jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.
|