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

Supported Databases

The list of databases supported by Hibernate is maintained at http://hibernate.org/80.html. You would be well advised to visit this site frequently, because it lists a variety of database-specific issues.

As of this writing, the following databases have been tested and are known to work with Hibernate:

  • IBM DB2 7.1, 7.2, 8.1

  • MySQL 3.23, 4.0

  • PostgreSQL 7.1.2, 7.2, 7.3, 7.4

  • Oracle 8i, 9i

  • Sybase 12.5 (JConnect 5.5)

  • Interbase 6.0.1 (Open Source) with Firebird InterClient 2.01

  • HypersonicSQL 1.61, 1.7.0

  • Microsoft SQL Server 2000

  • Mckoi SQL 0.93

  • Progress 9

  • Pointbase Embedded 4.3

  • SAP DB 7.3

The following databases have been used and are thought to be compatible:

  • Informix

  • Ingres

  • FrontBase

  • Firebird (1.5 with JayBird 1.01)

The feature of Hibernate most subject to per-database vagaries is the automatic generation of schema. Even if your database is not supported, you can probably generate a DDL that is close, and modify the schema to fit. If you wish to use a database that is not shown, you can create a new dialect (see the net.sf.hibernate.dialect.* package).

If you don't already have a database in mind, the popular open-source, cross-platform MySQL is an excellent choice.

NOTE

Throughout the rest of this book, all examples are shown as run against MySQL, installed as described below. With luck, you should be able to run all of the examples against your preferred database merely by adding your database's driver to the CLASSPATH and changing the JDBC connection parameters.


Introduction to MySQL

MySQL can be downloaded for free from the Web site http://www.mysql.com/ (Figure 1.8). You'll want to download two piecesthe server itself, and the driver.

Figure 1.8. MySQL Download


As of this writing, you can download the latest production version at http://www.mysql.com/downloads/mysql-4.0.html. On Windows, just click the download link for the version with installer. The instructions below should work on Windows NT, 2000, and XP. For other versions of Windows (and other operating systems), see the installation instructions on the MySQL Web site. Supported platforms include Linux, Solaris, FreeBSD, Mac OS X, and a variety of other flavors of UNIX (as well as a source release).

PLATFORM SUPPORT

I have personally used both Hibernate and MySQL on Windows, Red Hat Linux, and Mac OS X. If you are a Mac OS X developer, you may be interested in my other title, Mac OS X for Java Geeks (O'Reilly); it includes information on setting up Apache Jakarta Tomcat, and MySQL on Mac OS X. MySQL is a very common database on Linux; many distributions include it, and RPMs are available directly from the MySQL.com Web site.

While the screenshots and information shown in this text are generally provided for Windows, Mac OS X and Linux users should know that Hibernate works well on all of these platforms. While paths and screenshots shown were done on Windows, all of the software and tools covered in this text can easily be adapted to other platforms.


After downloading the resulting zip file (mysql-4.0.18-win.zip as of this writing), uncompress it using an unzip utility (such as WinZIP, http://www.winzip.com/). Inside the uncompressed folder, you should see several files. The only file of interest is the SETUP.EXE file. Double-click this and the installer will launch, as shown in Figure 1.9.

Figure 1.9. Starting the MySQL Installer


Click next several times, accepting the default options.

WARNING

Install MySQL in the default location, C:\mysql. Installation in another directory requires additional steps, not described here, even if you are using the MySQL installer.


After installation is complete, click Start -> Programs -> Accessories -> Command Prompt to bring up the terminal (on Windows XP; other versions of Windows may differ). At the terminal, use the CD command to change to the C:\mysql\bin directory. Then enter the command mysqld --install and press return (this will install MySQL as a Windows service).

C:\mysql\bin>mysqld --install
Service successfully installed.

Next, click Start -> Settings -> Control Panel -> Administrative Tools -> Services (again, on Windows XP; other versions of Windows may differ). This will bring up a window, as shown in Figure 1.10. Right-clicking on the entry for MySQL, you bring up the menu shown to start the server. Note that MySQL is set to Automatic in the Startup Type column; this means that MySQL will start automatically when you restart your computer. You may wish to double-click on the MySQL entry and change this value to Manual if you expect to use MySQL only rarelyjust remember that you'll need to come back to this pane to start the server before starting your development work.

Figure 1.10. Starting the MySQL Service


NOTE

Make sure that MySQL is started (the status column in the Services window should read "Started") before continuing.


Now that MySQL has been installed and configured, let's make sure that we can access the monitor. Returning to our command prompt, from inside the C:\mysql\bin directory, issue the command mysql u root and press return.

C:\mysql\bin>mysql -u root
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1 to server version: 4.0.18-max-debug

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql>

If you are familiar with SQL, you can now enter SQL commands, creating databases, tables, inserting and selecting records, and so on.

Before continuing, create a test database by entering the command create database hibernate and pressing return.

mysql> create database hibernate;
Query OK, 1 row affected (0.00 sec)

The only thing left to do is download the MySQL driver (called MySQL Connector/J). You can download the latest version of the driver from http://www.mysql.com/products/connector-j/. As of this writing, the production release is version 3.0, with the ZIP file download named mysql-connector-java-3.0.11-stable.zip. Inside this ZIP archive, you will find three folders, clover, META-INF, and mysql-connector-java-3.0.11-stable. Inside the mysql-connector-java-3.0.11-stable folder, you will find a mysql-connector-java-3.0.11-stable-bin.jar file. This is the file you should put on your CLASSPATH to connect to MySQL.

NOTE

These instructions create a minimal, insecure installation of MySQL on your system (including a default, local-host-only root account with no password). If you are using MySQL in a production environment, you'll want to do a lot more (beyond the scope of this text) to secure and optimize MySQL.


If you have configured your system in this fashion, you will use the configuration options shown in Table 1.2 to connect to MySQL. Note the hibernate string at the end of the connection URL; that's a reference to the database we just created.

Table 1.2. Default MySQL Connection Properties

Hibernate Property

Setting

hibernate.connection.driver_class

com.mysql.jdbc.Driver

hibernate.connection.url

jdbc:mysql://localhost/hibernate

hibernate.connection.username

root

hibernate.connection.password

none set to an empty string

hibernate.dialect

net.sf.hibernate.dialect.MySQLDialect


Now that you've set up and configured MySQL, you're ready to move on to the next chapter and start working with a sample Hibernate application.