Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
Although we’re going to start small in this walkthrough, once we start designing data structures and building Java classes and database tables that represent them, along with all the configuration and control files to glue them together and make useful things happen, we’re going to end up with a lot of files. So let’s start out with a good organization from the beginning. As you’ll see in this process, between the tools you’ve downloaded and their supporting libraries, there are already a significant number of files to organize.
This is especially worth doing from the outset because if you end up building something cool by following the examples in this book and end up wanting to turn it into a real application, you’ll already be in good shape. More to the point, if you set things up the way described here, the commands and instructions given throughout the examples will make sense and actually work. Examples build on one another throughout the PDF, so it’s important to get on the right track from the beginning.
If you want to skip ahead to a later example or just avoid typing some of the longer sample code and configuration files, you can download a “finished” version of the examples from http://examples.oreilly.com/hibshortcut/http://examples.oreilly.com/hibshortcut/ .
So, to get ready to experiment with HSQLDB and Hibernate:
Pick a location on your hard drive where you want to work through these examples, and create a new folder, which we’ll refer to from now on as your project directory.
Move into that directory, and create subdirectories called src, lib, tools, and data. The hierarchy of Java source and related resources will be in the src directory. Our build process will compile it into a classes directory it creates, as well as copy any runtime resources there. The data directory is where we’ll put the HSQLDB database.
The lib directory is where we’ll place third-party libraries we use in the project. For now, copy the HSQLDB and Hibernate JAR files into the lib directory: If you haven’t yet done so, expand the hsqldb ZIP file you downloaded. You’ll find hsqldb.jar in its lib directory; copy it to your own project lib directory (the lib directory you just created in your new project directory).
Similarly, locate the lib directory in the Hibernate directory you expanded in the previous section, and copy all of its contents into your own project lib directory (you’ll notice that Hibernate relies on a lot of other libraries; conveniently, they’re included in its binary distribution so you don’t have to hunt them all down yourself.) Then copy Hibernate itself, in the form of the hibernate3.jar file at the top level of the distribution, into your project lib directory.
NOTE
There are lots of pieces to copy into place here; attention to detail will be rewarded. Luckily, you can reuse your lib and tools directories in other Hibernate projects.
Installing the Hibernate Tools is a bit more tricky. Although they still support being used from the command line through Ant, as we’ll be doing in these examples, the primary focus of the tools has shifted toward use as a plug-in for the powerful Eclipse development environment. If you haven’t explored Eclipse yet, it’s probably worth your while to do so, but that’s way outside the scope of this PDF. So for now, here’s how to use the tools outside of Eclipse.
NOTE
This process may change, unfortunately. During technical review of this PDF, it changed drastically. It used to be possible to put the three Hibernate Tools JARs in the lib directory along with everything else and use your normal project class path, but a new beta of the tools came out during the review process, and the reviewers found that they could no longer use the tools by following my directions. It turns out the tools now use a custom-built Hibernate JAR and are incompatible with both the stable and beta JARs available on the Hibernate downloads site, so we need to set them up to use their own private Hibernate installation as described below. Hopefully this will all get much simpler someday. Supposedly the Tools authors plan to eventually focus again on making them easy to use from Ant, as noted at http://forum.hibernate.org/viewtopic.php?t=961987http://forum.hibernate.org/viewtopic.php?t=961987 . Today, even the online documentation is incorrect.
Locate the file hibernate-tools.jar inside the Hibernate Tools directory you expanded, and copy it into your own tools directory. It’s probably easiest to let your operating system find the file for you, because it’s nested in a deep path. (Currently, plugins/org.hibernate.eclipse_3.2.0.beta6a/lib/tools, but this will change with each release of the tools.)
Copy the other files contained in the same directory as hibernate-tools.jar into your tools directory. These are used by the Hibernate Tools and currently include freemarker.jar and jtidy-r8-21122004.jar.
Copy the entire hibernate directory that is sitting next to the directory from which you’ve been copying these tools (in other words, given the current plug-in structure, plugins/org.hibernate.eclipse_3.2.0.beta6a/lib/hibernate) into your project tools directory, too. This lets the Tools access their private, custom Hibernate version, which is currently required. Your tools directory should now contain:
% ls tools freemarker.jar hibernate-tools.jar hibernate/ jtidy-r8-21122004.jar
The example classes we’re going to create are all going to live in the com.oreilly.hh (harnessing Hibernate) package, so create these directories under the src directory. On Linux and Mac OS X, you can use:
mkdir -p src/com/oreilly/hh
from within your project directory to accomplish this in one step.
At this point, your project directory should be structured as in Figure 4-1.
The lib and tools/hibernate directories are collapsed because they contain so many files that the screenshot wouldn’t fit otherwise. Each of them contains an entire Hibernate distribution, and lib also contains HSQLDB. After following the above steps, using the stable release of Hibernate available at the time of this writing, lib contains the following files:
%
ls lib
_README.txt hsqldb.jar
ant-1.6.5.jar jaas.jar
ant-antlr-1.6.5.jar jaas.licence.txt
ant-junit-1.6.5.jar jacc-1_0-fr.jar
ant-launcher-1.6.5.jar jaxen-1.1-beta-7.jar
ant-swing-1.6.5.jar jboss-cache.jar
antlr-2.7.6rc1.jar jboss-common.jar
antlr.license.txt jboss-jmx.jar
apache.license-2.0.txt jboss-system.jar
apache.license.txt jdbc2_0-stdext.jar
asm-attrs.jar jdbc2_0-stdext.licence.txt
asm.jar jgroups-2.2.8.jar
c3p0-0.9.0.jar jta.jar
c3p0.license.txt jta.licence.txt
cglib-2.1.3.jar junit-3.8.1.jar
cleanimports.jar log4j-1.2.11.jar
commons-collections-2.1.1.jar oscache-2.1.jar
commons-logging-1.0.4.jar proxool-0.8.3.jar
concurrent-1.3.2.jar swarmcache-1.0rc2.jar
connector.jar syndiag2.jar
connector.licence.txt version.properties
dom4j-1.6.1.jar versioncheck.jar
ehcache-1.1.jar xerces-2.6.2.jar
hibernate3.jar xml-apis.jar