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).
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:
- Open session
- Start transaction
- Save/Load objects into the session
- Commit transaction
- 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.
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:
- Needs no-arg constructor
- Could be abstract or extend non persistent class
- Class cannot be final and no final methods (for Proxy generation)
For generating primary keys, Hibernate supports various types of ID generators to chose from.
- 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.
- Identity -This generator supports identity columns in DB2, MySQL, MS SQL Server, Sybase.
- Sequence - This generator supports sequences.
- 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.
- 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.
- Seqhilo - works like the regular hilo generator, except it uses a named database sequence to generate high values.
- 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.
- Guid - This generator provides a database-generated globally unique identifier string on MySQL and SQL Server.
- 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.
Post a Comment