Monday, November 4, 2013

Hibernate Tutorial - Part I


Hibernate is an Object relational mapping (ORM) tool which will help us map Java objects to the underlying database objects. It will help us perform various operations on objects and hibernate will translate it to the DBMS operations and saves us from writing sql queries.

An equally important aspect is the importance of object orientated principles which need not be compromised when we use Hibernate. If we go with plain old sql style of design, then due to the disconnect between the object oriented design and the DBMS design, often the object oriented design is compromised.

For instance, concepts like inheritance, composition do not have a corresponding representation in the DBMS world. Similarly, we cannot truly represent a bi-drectional navigation and mapping between two entities (say for eg, Author and Book) in the DBMS world. These short comings could be overcome if we use Hibernate and we can minimize the influence of DBMS design on the object oriented design.

When we use Hibernate, we can map the entity classes (i.e POJO classes) to the corresponding database tables and the fields of the classes relate to the columns (explained in detail as we move along).

Hibernate Session:

Session is a single threaded object which keeps track of all the persistent objects within it. It offers CRUD operations for the mapped entity classes.  A transaction starts and ends within a session.

So a typical psuedo code while using session will look like:

  1. Open session
  2. Start transaction
  3. Save/Load objects into the session
  4. Commit transaction
  5. Close session
A session is obtained from a Session factory which is a thread safe object. The factory could be initialized through a Configuration object, by providing the connection details of the database. The configuration could be provided as a xml or as a properties file.
Hibernate supports use of third party connection pools which could be plugged in seamlessly.
C3p0 is a popular connection pool provider which could be used.

Entity Mappings:

The entity to schema mapping could be either provided as a xml file or as annotations within the entity class. We will be using annotations in the examples in the following posts.

The entity objects or the POJOs should follow the following rules:
  1. Needs no-arg constructor
  2. Could be abstract or extend non persistent class
  3. Class cannot be final and no final methods (for Proxy generation)

ID Generators:

For generating primary keys, Hibernate supports various types of ID generators to chose from.
  1. Native  - The native identity generator picks other identity generators like identity,sequence, or hilo, depending on the capabilities of the underlying database. Use this generator to keep your mapping metadata portable to different database management systems.
  2. Identity -This generator supports identity columns in DB2, MySQL, MS SQL Server, Sybase.
  3. Sequence - This generator supports sequences.
  4. Increment - This generator reads the maximum (numeric) primary key column value of the table and increments the value by one each time a new row is inserted.
  5. Hilo - A high/low algorithm is an efficient way to generate identifiers of type long, given a table and column (by default hibernate_unique_key and next, respectively) as a source of high values. The high/low algorithm generates identifiers that are unique only for a particular database. High  values are retrieved from a global source and are made unique by adding a local low value.
  6. Seqhilo - works like the regular hilo generator, except it uses a named database sequence to generate high values.
  7. Uuid.hex - generates a unique value based on appending the following values:IP Address,the startup time of current JVM,the current time,and counter value.It support all database and has no paameter.
  8. Guid - This generator provides a database-generated globally unique identifier string on MySQL and SQL Server.
  9. Select -This generator retrieves a primary key assigned by a database trigger by selecting the row by some unique key and retrieving the primary key value. An additional unique candidate key column is required for this strategy,and the key option has to be set to the name of the unique key column.
In the following posts, we will take a look at the various features of Hibernate along with code examples.


No comments:

Post a Comment