Cooking Up a Schema
That was pretty easy, wasn't it?
You'll be happy to learn that creating database
tables is a very similar process. As with code generation,
you've already done most of the work in coming up
with the mapping document. All that's left is to set
up and run the schema generation tool.
How do I do that?
The first step
is something we alluded to in Chapter 1. We
need to tell Hibernate the database we're going to
be using, so it knows the specific
"dialect" of SQL to use. SQL is a
standard, yes, but every database goes beyond it in certain
directions and has a specific set of features and limitations that
affect real-life applications. To cope with this reality, Hibernate
provides a set of classes that encapsulate the unique features of
common database environments, in the package
net.sf.hibernate.dialect. You
just need to tell it which one you want to use. (And if you want to
work with a database that isn't yet supported
"out of the box," you can implement
your own dialect.)
In our case, we're working with HSQLDB, so we want
to use HSQLDialect. The easiest way to
configure Hibernate is to create a
properties file named hibernate.properties and
put it at the root level somewhere in the class path. Create this
file at the top level of your src directory, and
put the lines shown in Example 2-4 into it.
NOTE
You can use an XML format for the configuration information as well, but for the simple needs we have here, it doesn't buy you anything.
Example 2-4. Setting up hibernate.properties
1 hibernate.dialect=net.sf.hibernate.dialect.HSQLDialect
2 hibernate.connection.driver_class=org.hsqldb.jdbcDriver
3 hibernate.connection.url=jdbc:hsqldb:data/music
4 hibernate.connection.username=sa
5 hibernate.connection.password=
In addition to establishing the SQL dialect we are using, this tells
Hibernate how to establish
a
connection to the database using the JDBC driver that ships as part
of the HSQLDB database JAR archive, and that the data should live in
the data directory we've
created—in the database named music. The
username and empty password (indeed, all these values) should be
familiar from the experiment we ran at the end of Chapter 1.
TIP:
Notice that we're using a relative path to specify
the database filename. This works fine in our examples —
we're using ant to control the working directory. If
you copy this for use in a web application or other environment,
though, you'll likely need to be more explicit about
the location of the file.
You can put the properties file in other places, and give it other
names, or use entirely different ways of getting the properties into
Hibernate, but this is the default place it will look, so
it's the path of least resistance (or, I guess,
least runtime configuration).
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.