Hibernate and other ORM

PERSISTENCE


  • Ensure that state of object is accessible even after the application stops working.
  • Traditionally flat files were used to persist data.
  • File system has:
Lack of consistency.
Searching is difficult and time consuming.
Limited support to concurrent access.
So we move to databases(Relational database).

Problems with Jdbc 

Jdbc code is tightly coupled to the business logic in java class.
Code relies heavily on SQL.
Migrating from one database to another is difficult.
Relational databases doesn't address OOPS concepts.
Do not provide the required type support.

ORM


   Known as Object Relational Mapping.
Persists application objects to the tables in a relational database.
Hiding the underlying database architecture.
Converting data between relational databases and object oriented programming languages.

ORM FRAMEWORKS

Enterprise JavaBeans Entity Beans.
Castor.
Top Link.
Hibernate.
MyBatis formerly named iBatis.
Spring DAO ,etc.

HIBERNATE History


Started in 2001 by Gavin King.
Open source ORM tool.
Implementation of JPA.
Maps:
Java Class name   ----->  Table name.
Class fields           ----->  Table Columns.
Fields datatypes    ----->  Column Datatypes

WHY HIBERNATE

Makes object-relational mapping simple.
XML file defines the table that needs to be mapped to a particular class.
No need to modify application class to achieve OR mapping.
No need to worry about database changes.
Easy to navigate from db to db (Just change the dialect).
Supports many databases like
MySql, Oracle, Sybase, Derby

Classes And Interfaces

SessionFactory 
Interface that is available in org.hibernate package.
Helps in creation of Session instances.
Threads serving client request obtain sessions from factory.
Internal state is immutable.
One per database.

Session 
Interface present in org.hibernate package.
Key interface between Java application and Hibernate.
Lifecycle is bound by beginning and ending of transaction.
Short-Lived object.
Offer create, read, delete operations for instances.

Configuration
Class present in org.hibernate.cfg package.
Allows the application to specify mapping files.
Used to create session factory via buildSessionFactory().
Usually single instance gets created.


Transaction
Interface present in org.hibernate package.
Associated with a Session.
Usually initiated by a call to beginTransaction() method.
commit() method is used to end the transaction.

Query 
Interface present in org.hibernate package.
Instance gets created by calling Session.createQuery().
Lifespan is bound to the session lifespan.
Used to perform query (HQL).
Gets executed by calling list(), scroll() or iterate().


HQL

Hibernate Query Language.
Quite similar to SQL.
HQL uses class name instead of table name, and property names instead of column name.
Insert query doesn't have VALUES keyword.
Also supports insertion of values from other table like:

"insert into Object (id, name) select oo.id, oo.name from OtherObject oo";

IBATIS

Is SQL Centric Tool.
Currently InActive.
MyBatis is currently used.
Direct control over SQL queries.
Used when you need to write complicated sql queries.
Basically used when only fetching is required.
Maps result set with objects.

DIFFERENCES


Ibatis maps ResultSet to POJO Objects. Hibernate maps POJO Objects to database tables.
Use of Store Procedure is quite easy in Ibatis rather than Hibernate.                  
Ibatis use SQL(quite dependent on database) and Hibernate use HQL(quite independent of database).
In Ibatis user have to spend time on writing SQL.

Basic Code Examples




No comments:

Post a Comment