Tutorial: Introduction to Hibernate - Hibernate
Home » Hibernate, Tech Notes

Hibernate ORM Basics

19 February 2009 One Comment

Introduction to ORM concepts:

Object Relational Mapping is mapping of objects to tables in a relational database. Mapping is done using metadata that describes relation between object attributes and table columns. This mapping also takes care of the cardinality of relations amongst the objects and rows of tables.

There are many ORM tools available in market e.g. hibernate, toplink, and recently added Java Persistent API. These tools try to encapsulate technology required to interact with any database. Some of the services offered by an ORM tool are -

– Performing basic database operations like create, read, update, and delete (CRUD)

– Ability to support native query languages

– Alternative query language (Not necessary)

– Syntax to define object mapping with database tables

– Support to handle dirty, lazy fetching of objects

Hibernate has evolved enough to support most of the ORM requirements. Also, it maps object from two common technologies, Java and .NET to tables from many databases e.g. Oracle, MySql, etc.

Introduction to Hibernate:

Hibernate provides support to the most of services required to interact with a database. These services involve connecting to database, executing and managing transactions with data entities and supporting additional Java application services. Following diagram explains role of Hibernate while interacting with a database.

Hibernate Architecture

Hibernate Architecture

Important Terms:

Session Factory

It holds thread safe cache of instance of database connection mappings. It also works as a factory to create session. It is a client of connection provider i.e. programs that can use session factory to make connection to database through a selected connection provider (e.g. JDBC).

Session

This is a single threaded light weight object representing conversation between application and database. This object represents a JDBC connection to the database.

Transaction

Transaction represents an atomic operation in database interaction. This is a short lived single threaded object in hibernate.

Persistent Objects

These are single threaded objects representing persistent state having a short lifecycle. Persistent objects are associated with only one session at a time.

Transient Objects

As name suggests, these are the objects which are created by application but these are not persisted in database. Generally, these do not contain the identifier (primary key) attribute unless persisted.

Detached Objects

These objects are retrieved from database but not connected with any session. Usually when objects travel to higher layers (business, web tiers), they get detached from session.

Sample Code:

Following code is an example of code that uses hibernate to persist an object in database.

Requirements

– Add hibernate.jar to classpath

– Create hibernate.cfg.xml file

– Create .hbm file

– Create object class mapping to database entity

– Create calling program

Configurations

Basic hibernate configurations required are Hibernate.cfg.xml and .hbm file mapping objects and tables.

hibernate.cfg.xml

 

hibernate.cfg.xml

hibernate.cfg.xml

Configurations defined in this file mainly deal with making connection with database and application services related to the database interactions. It contains database connection attributes (driver name, username, password). It also contains attributes related to transaction, whether the SQL executed to be shown or not etc. In addition to this, it contains details of hbm files.

.hbm file

cat.hbm.xml

cat.hbm.xml

It contains mapping of object attributes with database columns. Each class mapping must contain the mapping of attribute ‘id’ with details of generation of this id.

Object Mapping to Database Entity

Following class can be used map database entity.

Cat.java

Cat.java

Calling Code

Following code can be used to persist above object in database.

 

Hibernate Code

Hibernate Code

 

More Related Posts in Hibernate, Tech Notes

One Comment »

  • syam kumar mathukumilli said:

    Hi,
    very good tutorial. This will be very helpful and useful for people who wants to learn Hibernate and improve skills in hibernate

Leave your response!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

This is a Gravatar-enabled weblog. To get your own globally-recognized-avatar, please register at Gravatar.