Hibernate is an object/relational mapping service that bridges the worlds of Java objects and relational databases. If this sounds like it might be useful to you, keep reading. This article is excerpted from chapter one of Hibernate A Developer's Notebook, written by James Elliot (O'Reilly; ISBN: 0596006969).
Installation and Set Up of Hibernate - Setting Up a Project Hierarchy (Page 5 of 6 )
Although we’re going to start small, 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.
Why do I care?
If you end up building something cool by following the examples in this book, and want to turn it into a real application, you’ll be in good shape from the beginning. More to the point, if you set things up the way we describe here, the commands and instructions we give you throughout the examples will make sense and actually work. Many examples alsobuild on one another throughout the book, 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 “finished” versions of the chapter examples from the book’s web site. These downloads will all be organized as described here.
How do I do that?
Here's how:
Pick a location on your hard drive where you want to play with Hibernate, and create a new folder, which we’ll refer to from now on as your project directory.
Move into that directory, and create subdirectories calledsrc,lib, anddata. The hierarchy of Java source and related resources will be in thesrc directory. Our build process will compile it into aclasses directory it creates, as well as copy any runtime resources there. Thedata directory is where we’ll put the HSQLDB database, and any Data Definition Language (DDL) files we generate in order to populate it.
Thelib directory is where we’ll place third-party libraries we use in the project. For now, copy the HSQLDB and Hibernate JAR files into thelib directory.
If you haven’t already done so, expand the HSQLDB distribution file you downloaded earlier in this chapter. You’ll findhsqldb.jar in itslib directory; copy this to your own projectlib directory (thelib directory you just created in step 2).
Similarly, locate thelib directory in the Hibernate directory you expanded in the previous section, and copy all of its contents into your own projectlib 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 thehibernate2.jar file found at the top level of the distribution, into your projectlib directory.
Installing the Hibernate Extensions is very similar. Locate thetools/lib directory inside the Hibernate Extensions directory you expanded, and copy its contents into your ownlib directory, so the extensions will be able to access the libraries they rely on.
Finally, copy the extensions themselves, which are in the filehibernate-tools.jar (found in thetools directory), into yourlib directory.
There are lots of pieces to copy into place here; attention to detail will be rewarded. Luckily, you can reuse your lib directory in other Hibernate projects.
The example classes we’re going to create are all going to live in thecom.oreilly.hh (harnessing Hibernate) package, so create these directories under thesrc 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 shown in Figure 1-1.
Figure 1-1. Initial project directory contents
The lib directory is collapsed because it contains so much that the screen shot wouldn’t fit otherwise. After following the above steps, using the release of Hibernate available at the time of this writing, it contains the following files: