O'Reilly logo

Harnessing Hibernate by James Elliott, Ryan Fowler, Timothy M. O'Brien

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 1. Installation and Setup

It continues to amaze me how many great, free, open source Java™ tools are out there. When I needed a lightweight object/relational mapping service for a JSP e-commerce project at the turn of the millennium, I had to build my own. It evolved over the years, developed some cool and unique features, and we’ve used it in a wide variety of different contexts. But, once I discovered Hibernate, we used that for new development instead of my own familiar system (toward which I’ll cheerfully admit bias). That should tell you how compelling it is!

If you’re looking at this book, you’re likely interested in a powerful and convenient way to bridge the worlds of Java objects and relational databases. Hibernate fills that role very nicely, without being so complicated that learning it becomes a daunting challenge in itself. To demonstrate that, this chapter guides you to the point where you can play with Hibernate and see for yourself why it’s so exciting.

Later chapters will look at using Hibernate as part of more complex environments such as Spring and Stripes, as well as using it with other databases. The goal of this first chapter is to show you how easy it is to put together a basic, self-contained environment in which you can explore Hibernate and do real things with it.

Getting an Ant Distribution

Although it might be surprising, the first few things you need to get Hibernate running have nothing to do with Hibernate itself. First, you must set up an environment in which the remaining examples work. This will have the pleasant side effect of building a solid foundation for any actual project you might be undertaking.

If you’re not already using Ant to manage the building, testing, running, and packaging of your Java projects, now is the time to start. The examples in this book are Ant-driven, so you’ll need a working Ant installation to run them and experiment with variations on your own system, which is the best way to learn.

First of all, get an Ant binary and install it.

Why do I care?

The examples use Apache Ant for several reasons. It’s convenient and powerful; it’s one of the standard build tools for Java-based development; it’s free; and it’s cross-platform. If you use Ant, the examples will work equally well anywhere there’s a Java environment, which means readers of this book won’t be frustrated or annoyed. Fortunately, it also means we can do many more cool things with less effort—especially since several Hibernate tools have explicit Ant support, which I’ll show you how to leverage. (I should note that these days more complex Java projects often use Maven, which adds many other project management capabilities. Since I had to pick one, in the spirit of keeping things simple and true to what I find useful, I went with Ant for these examples.)

If you are currently using Maven as a build tool, you will notice that we are using Maven’s Ant Tasks to manage dependencies from our Ant builds. Although Maven is gaining momentum, Ant continues to be the most widely used build tool in Java development. Every chapter’s example code folder also has a Maven pom.xml file and can be compiled with Maven. In many cases, the Maven build file provides the same functionality as the Ant build.xml file by using the Maven Hibernate3 plug-in. In Chapter 12 you will find some guidance for building and deploying Hibernate applications using full-blown Maven, but the majority of the examples in this book focus on Ant as a build tool, using the Maven Ant Tasks to relieve the tedium of finding and downloading the various libraries we need, and the libraries on which they, in turn, rely.

To take advantage of all these capabilities, you need to have Ant installed and working on your system.

Note

I used to wonder why people bothered with Ant when they could use Make. Now that I’ve seen how well Ant manages Java builds, I feel lost without it.

How do I do that?

You can download a binary release of Ant from http://ant.apache.org/bindownload.cgi. Scroll down to find the current release of Ant, and download the archive in a format that’s convenient for you to work with. Pick an appropriate place for it to live, and expand the archive there. The directory into which you’ve expanded the archive is referred to as ANT_HOME. Let’s say you’ve expanded the archive into the directory /usr/local/apache-ant-1.7.0; you may want to create a symbolic link to make it easier to work with and to avoid the need to change any environment configuration when you upgrade to a newer version:

/usr/local % ln -s apache-ant-1.7.0 ant

Once Ant is situated, you need to do a couple of things to make it work correctly. You need to add its bin directory in the distribution (in this example, /usr/local/ant/bin) to your command path. You also need to set the environment variable ANT_HOME to the top-level directory you installed (in this example, /usr/local/ant). Details about how to perform these steps under different operating systems can be found in the Ant manual (http://ant.apache.org/manual/) if you need them.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required