36 articles and counting
      

Integration and Configuration of Hibernate

You do not have to run Hibernate inside any particular J2EE container or
framework—Hibernate 3 only requires Java 2 Standard Edition (J2SE), version 1.3 or greater,
although the new Annotations feature requires J2SE 5.0 (or later).

The Hibernate distribution
includes a large set of libraries which you need to place on your application’s classpath. You can call Hibernate from your Java application directly, or you can access Hibernate
through another framework such as Spring. Typically, you
would use Hibernate to either create a data access layer for an application or replace an
existing data access layer. The Spring framework provides excellent Hibernate
integration, including generic support for persistence objects, a generic set of persistence exceptions, and transaction management.

To get any Hibernate application to work, you have to follow a set of basics:

  1. Set
    up the database if it does not exist already. You can use Hibernate’s schema export tool to create your database schema (if you wish) from your mapping files
  2. Add the Hibernate Java libraries to your application’s classpath
  3. Identify the plain old Java objects (POJOs) in your application that require persistence – and specifically their particular properties that represent fields in the database tables
  4. Create Hibernate XML mapping files for each of the POJOs that map properties to
    columns in a table
  5. Create a Hibernate XML configuration file that points to your database and your XML
    mapping files
  6. In your Java application, create a Hibernate Configuration object that references your
    XML configuration file
  7. In your Java application, build a Hibernate SessionFactory object from the
    Configuration object
  8. Finally, retrieve Hibernate Session objects from the SessionFactory, and write your data
    access logic for your application (create, retrieve, update, and delete)

Running Hibernate

Regardless of the environment that you are integrating Hibernate into, certain requirements
remain constant. You will need to define the configuration details that apply—these are then
represented by a Configuration object. From the Configuration object, a single SessionFactory object is created; and from this, Session objects are instantiated, through which your application
accesses Hibernate’s representation of the database.

Deploying Hibernate

To integrate Hibernate with your Java application, you will need to use several Java libraries.

The first library is the Java Archive (JAR) file for your JDBC driver, which you will need to find
for your specific relational database. The Hibernate download does not include any JDBC
drivers.
Because every relational database—and hence every JDBC driver—behaves slightly differently,
the Hibernate team created dialects to abstract away the differences. These dialects define
the SQL variant and the specific database features to use for each vendor’s database. Every project
that uses Hibernate must specify one dialect in the Hibernate configuration file.

Once you have configured the JDBC driver, your next step is to deploy hibernate3.jar
with your application. This JAR file is provided with the Hibernate 3 binary distribution. The
file contains the classes in the org.hibernate package, along with several DTD and XML
Schema files. You will then need to deploy the other libraries that hibernate requires. These libraries are included in
the \lib directory of your Hibernate 3 installation. The purpose of each library is detailed in the README file, which also states
which libraries are optional and which are required.