More Books
Hibernate: A J2EE Developer's Guide
Hibernate: A J2EE™ Developer's Guide
Table of Contents
Copyright
Acknowledgments
About the Author
Preface
Required Skills
Roadmap
Chapter 1. Overview
Why Object/Relational Mapping?
What Is Hibernate?
Comparing JDBC to Hibernate
Hibernate's Mapping System
Other Java/Database Integration Solutions
How to Obtain and Install
Supported Databases
Chapter 2. Getting Oriented
Application Architecture
Mapping Files
Generating Java Source
Application Configuration
Web Application
JSP Interface
Chapter 3. Starting from Java
Java Object Model
Generated Mapping Files
Generated Schema
Working with Artifacts and Owners
Chapter 4. Starting from an Existing Schema
Initial Schema
Using Middlegen
Generated Mapping Files
Generated Java
Working with the Database
Chapter 5. Mapping Files
Basic Structure
Mapping File Reference
Chapter 6. Persistent Objects
Sessions
Objects and Identity
Life-Cycle Methods
Chapter 7. Relationships
Database Relationships
Java Collection Relationships
Java Class Relationships
Any-Based Relationships
Bi-directional Relationships
Chapter 8. Queries
HQL
HQL Reference
Select
From
Where
Group By
Having
Order By
Criteria Queries
Native SQL Queries
Chapter 9. Transactions
Introduction to Transactions
Optimistic and Pessimistic Locking
Chapter 10. Performance
Finding and Solving Problems
Queries
Inserts
Connection Pooling
Caching
Chapter 11. Schema Management
Updating an Existing Schema
Generating Update and Drop Scripts
Chapter 12. Best Practices, Style Guide, Tips and Tricks
Reducing Code with Inversion of Control
Reducing Session Creation Impact with ThreadLocal
Using Hibernate as an EJB BMP Solution
Integrating with Other Technologies
Applications That Use Hibernate
Strategies for Getting Started
Chapter 13. Future Directions
Hibernate 3.0
EJB 3.0
Here and Now
Index
SYMBOL
A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
Q
R
S
T
U
V
W
X

How to Obtain and Install

Hibernate can be downloaded for free from http://www.hibernate.org/. The core Hibernate distribution includes both Hibernate itself and several ancillary libraries. In addition to the main distribution, we will also make extensive use of the tools available in the Hibernate Extensions library.

Hibernate Distribution

Hibernate is distributed as a single compressed archive. For the remainder of this book, we will assume that you have decompressed the archive inside a folder called C:\devenv\. All screenshots and code in the book will assume that directory as the default installation directory for Hibernate and Hibernate Extensions.

Figure 1.4. Hibernate.org Website


DIRECTORY CONVENTIONS

On my Windows development system, I install downloaded libraries in a single directory, C:\devenv\. This represents my personal repository of downloaded libraries (and associated documentation, samples, etc.). My development source trees are stored in another directory, C:\devrep\, which is controlled by a version control system (CVS, soon to be converted to Subversion). As libraries are required by projects, I copy the .jar files from the C:\devenv\directory into the appropriate location in C:\devrep\. Therefore, project-specific dependencies are resolved by relative paths, but on occasion interim development is done with references to the C:\devenv\directory. I don't use the C:\Documents and Settings\<username> directory because too many packages are confused by spaces in paths.

On UNIX systems, I generally use ~\devrep\and ~\devenv\.

There's nothing very magical about this strategy, but it does keep my tools and documentation nicely sorted from my development tree.


Let's explore the Hibernate installation a bit, as shown in Figure 1.5. The bin directory contains a couple of batch scripts for generating schema from a *.hbm.xml file. The doc directory contains a copy of the Hibernate documentation, including both the reference material and the Hibernate javadoc (also available online at http://www.hibernate.org/). The eg directory contains a Hibernate-provided example. The lib directory contains all of the various libraries that Hibernate either requires or can potentially take advantage of. The src directory contains the source for Hibernate itself. The test directory contains a suite of JUnit tests, useful when building or developing Hibernate itself. Similarly, the build.bat and build.xml files are used to generate your own version of Hibernate from source (a batch file to kick off the build, and an Ant script, respectively). The changelog.txt file indicates updates to Hibernate made since the last major revision. The hibernate2.jar file, is, of course, the main Hibernate library. The file hibernate_logo.gif is simply the logo for Hibernate; you might want to put this on your Web site, with a link back to http://www.hibernate.org/. The lgpl.txt file contains the LGPL license for Hibernate, and finally, readme.txt contains a variety of useful information tidbits on Hibernate.

Figure 1.5. Hibernate Distribution


LGPL and Hibernate

"Hibernate is Free Software. The LGPL license is sufficiently flexible to allow the use of Hibernate in both open source and commercial projects. Using Hibernate (by importing Hibernate's public interfaces in your Java code), and extending Hibernate (by subclassing) is considered by the authors of Hibernate to be dynamic linking. Hence our interpretation of the LGPL is that the use of the unmodified Hibernate source or binary does not affect the license of your application code.

If you modify Hibernate and redistribute your modifications, the LGPL applies."

- From the Hibernate documentation, http://hibernate.org/196.html

In other words, if you download the Hibernate source, make modifications, and distribute the resulting binaries, you will need to publish the source for the modifications. If you are merely including the original Hibernate libraries with your application, commercial or otherwise, you are under no obligation to release your source.


Depending on your application, you'll need to include the main hibernate2.jar file and supporting libraries to your application's CLASSPATH.

The lib directory of Hibernate contains a large number of libraries. Table 1.1 shows a list of the libraries files included with Hibernate 2.1.2, along with a list of URLs for the original library developers and the use of the library. The latest version of Hibernate, 2.1.5, relies on the same JAR files, but in certain cases a later version is included. Generally speaking, it's easiest to simply rely on the versions included with Hibernate, but you may wish to visit the URL below for additional documentation on that particular component.

Table 1.1. Hibernate Support Libraries

Library

URL

Description

ant-1.5.3.jar

http://ant.apache.org/

Build tool (similar to make)

ant-optional-1.5.3.jar

http://ant.apache.org/

Supporting features for Ant

c3p0-0.8.3.jar

http://sourceforge.net/projects/c3p0

Database connection pool

cglib-2.0-rc2.jar

http://cglib.sourceforge.net/

Runtime code-generation library

commons-collections-2.1.jar

http://jakarta.apache.org/commons/collections/

Additional collections beyond JDK collections

commons-dbcp-1.1.jar

http://jakarta.apache.org/commons/dbcp/

Database connection pool

commons-lang-1.0.1.jar

http://jakarta.apache.org/commons/lang/

Wide variety of basic utility functions

commons-logging-1.0.3.jar

http://jakarta.apache.org/commons/logging/

Wrapper for a wide variety of logging systems

commons-pool-1.1.jar

http://jakarta.apache.org/commons/pool/

Generic object pool

concurrent-1.3.2.jar

http://gee.cs.oswego.edu/dl/classes/EDU/

Doug Lea's concurrent programming utilities

oswego/cs/dl/util/concurrent/intro.html

 

connector.jar

http://java.sun.com/j2ee/connector/

Sun specification for integrating enterprise systems.

dom4j-1.4.jar

http://dom4j.org/

Library to ease use of XML, XPath, XSLT.

ehcache-0.6.jar

http://sourceforge.net/projects/ehcache

In-memory and disk cache, specifically designed for use with Hibernate

jaas.jar

http://java.sun.com/products/jaas/

Java authentication and authorization service

jboss-cache.jar

http://jboss.org/products/jbosscache

JBoss distributed cache implementation

jboss-common.jar

http://jboss.org/

JBoss utilities, including network and logging

jboss-jmx.jar

http://jboss.org/

JBoss JMX implementation

jboss-system.jar

http://jboss.org/

JBoss core, including server and deployment engine

jcs-1.0-dev.jar

http://jakarta.apache.org/turbine/jcs/

Distributed cache system

jdbc2_0-stdext.jar

http://java.sun.com/products/jdbc/

JDBC 2.0 Optional Package, server side data sources

jgroups-2.2.jar

http://www.jgroups.org/

Toolkit for reliable multicast

jta.jar

http://java.sun.com/products/jta/

Java Transaction API, distributed transactions

junit-3.8.1.jar

http://www.junit.org/

Regression testing framework

log4j-1.2.8.jar

http://logging.apache.org/log4j/

Logging framework

odmg-3.0.jar

http://www.odmg.org/ originally, now source can be found at http://db.apache.org/ojb/

ODMG is now considered deprecated in favor of JDO and will probably be removed from a future version of Hibernate

oscache-2.0.jar

http://www.opensymphony.com/oscache/

Cache system

proxool-0.8.3.jar

http://proxool.sourceforge.net/

Database connection pool

swarmcache-1.0rc2.jar

http://swarmcache.sourceforge.net/

Distributed cache system

xalan-2.4.0.jar

http://xml.apache.org/xalan-j/

XSLT processor

xerces-2.4.0.jar

http://xml.apache.org/xerces2-j/

XML parser

xml-apis.jar

http://xml.apache.org/xerces2-j/

JAXP support


The precise collection of libraries you'll need will depend on your use of Hibernate. Aside from space, there's no reason not to copy all of the libraries into your application's path (for example, your \WEB-INF\lib\directory for a Web application).

Hibernate Extensions Distribution

In addition to the core Hibernate distribution, you should also download and uncompress the Hibernate Extensions library (as of this writing, version 2.0.2). Figure 1.6 shows the distributions.

Figure 1.6. Hibernate Extensions Distribution


You may notice that the Hibernate Extensions distribution really consists of two main components: the Hibern8 IDE and a suite of command-line tools. Hibern8 IDE is really more analogous to a SQL monitor tool, allowing you to interactively query and explore object-results, as shown in Figure 1.7. You can use Hibern8 to interactively build your HQL queries, as shown in Chapter 8.

Figure 1.7. Hibern8 IDE


The various items in the tools directory are used to for mechanically generating files. The utility class2hbm is used to generate an hbm.xml file from compiled .class files. This is useful but not a complete solution, because hand-editing of hbm.xml files is still required; instead this book will be focus on the more complete solution provided by XDoclet, as described in Chapter 3. The utility ddl2hbm allows for import of an existing schema and automatic generation of an hbm.xml file, although this use has been deprecated in favor of a tool called MiddleGen, as will be discussed in Chapter 4. Finally, hbm2java generates Java code for you automatically from an existing hbm.xml file, as will also be discussed in Chapter 2 and Chapter 4.

Configuration

As we have seen, Hibernate is essentially a clump of librariesnot a full application. In the next chapter, we'll show a fully working Hibernate application, with complete instructions on how to set it up and configure it. If you already have a Hibernate-supported database installed and working, you can skip to Chapter 2 and get started with Hibernate. If you don't have a database set up (or aren't sure if it's supported), continue to the next section for instructions and information on how to get started with a Hibernate-supported database.