SlideShare a Scribd company logo
By Harshit Rastogi
It is ORM (Object –Relation Mapping) Tool It uses POJO objects (Plain Old Java Objects) No direct interaction with the database.
SessionFactory – Is a factory which provides Session. Session – Single unit of work. Dirty checking Hibernate xml file – ‘.hbm’ It uses reflection
.  A typical POJO class
Event.hbm.xml <hibernate-mapping> <class name=&quot;events.Event&quot; table=&quot;EVENTS&quot;> <id name=&quot;id&quot; column=&quot;EVENT_ID&quot;> <generator class=&quot;native&quot;/> </id> <property name=“name&quot;/> <property name=&quot;address&quot; column=&quot;ADDRESS&quot;/> </class> </hibernate-mapping>
hibenrate.hbm.xml
Singleton class to create SessionFactory object
 
Get() - Return the persistent instance of the given entity class with the given identifier, or null if there is no such persistent instance.  Load() - Return the persistent instance of the given entity class with the given identifier, assuming that the instance exists Flush() - Force this session to flush  Delete() - Remove a persistent instance from the datastore.  Contains() -  Check if this instance is associated with this Session.  Persist() -Make a transient instance persistent.  Evict() - Remove this instance from the session cache() beginTransaction() closeTransaction()
Execute complex queries containing conditions where clauses. Criteria Interface is the option. Some typical SQL example: Select * from cat where name like “Fritz%” and weight between + minweight + and + maxweight Equivalent hibernate query: List cats = sess.createCriteria(Cat.class) .add( Restrictions.like(&quot;name&quot;, &quot;Fritz%&quot;) ) .add( Restrictions.between(&quot;weight&quot;, minWeight, maxWeight) ) .list();
List cats = sess.createCriteria(Cat.class)  .add( Restrictions.like(&quot;name&quot;, &quot;Fritz%&quot;) )  .add( Restrictions.or( Restrictions.eq( &quot;age&quot;, new Integer(0) ), Restrictions.isNull(&quot;age&quot;) ) ) .list();  List cats = sess.createCriteria(Cat.class)  .add( Restrictions.in( &quot;name&quot;, new String[] { &quot;Fritz&quot;, &quot;Izi&quot;, &quot;Pk&quot; } ) ) .add( Restrictions.disjunction() .add( Restrictions.isNull(&quot;age&quot;) ) .add( Restrictions.eq(&quot;age&quot;, new Integer(0) ) ) .add( Restrictions.eq(&quot;age&quot;, new Integer(1) ) ) .add( Restrictions.eq(&quot;age&quot;, new Integer(2) ) ) ) ) .list();
table per class hierarchy  table per subclass  table per concrete class
Exactly one table is required
Each class has its own table with the primary key related to the main class. Four tables are involved.
Each table defines columns for all properties of the class, including inherited properties.  Three tables are involved for the subclasses
Four ways of achieving mapping the in RDBMS. One-to-one One-to-many Many-to-one Many-to-many Association can be unidirectional or bi-directional.
One person can have only one address Table schema create table Person ( personId bigint not null primary key, addressId bigint not null unique ) create table Address ( addressId bigint not null primary key )
Many people sharing the same address Table Schema create table Person ( personId bigint not null primary key, addressId bigint not null ) create table Address ( addressId bigint not null primary key )
One person having many places to stay. Table Schema  create table Person ( personId bigint not null primary key ) create table Address ( addressId bigint not null primary key, personId bigint not null )
Caching can be done at various level. First level caching is done by session Second level can be done using cache frameworks Hibernate supports various implementation EHCache OSCache   SwarmCache   JBoss TreeCache

More Related Content

Introduction to hibernate

  • 2. It is ORM (Object –Relation Mapping) Tool It uses POJO objects (Plain Old Java Objects) No direct interaction with the database.
  • 3. SessionFactory – Is a factory which provides Session. Session – Single unit of work. Dirty checking Hibernate xml file – ‘.hbm’ It uses reflection
  • 4. . A typical POJO class
  • 5. Event.hbm.xml <hibernate-mapping> <class name=&quot;events.Event&quot; table=&quot;EVENTS&quot;> <id name=&quot;id&quot; column=&quot;EVENT_ID&quot;> <generator class=&quot;native&quot;/> </id> <property name=“name&quot;/> <property name=&quot;address&quot; column=&quot;ADDRESS&quot;/> </class> </hibernate-mapping>
  • 7. Singleton class to create SessionFactory object
  • 8.  
  • 9. Get() - Return the persistent instance of the given entity class with the given identifier, or null if there is no such persistent instance. Load() - Return the persistent instance of the given entity class with the given identifier, assuming that the instance exists Flush() - Force this session to flush Delete() - Remove a persistent instance from the datastore. Contains() -  Check if this instance is associated with this Session. Persist() -Make a transient instance persistent. Evict() - Remove this instance from the session cache() beginTransaction() closeTransaction()
  • 10. Execute complex queries containing conditions where clauses. Criteria Interface is the option. Some typical SQL example: Select * from cat where name like “Fritz%” and weight between + minweight + and + maxweight Equivalent hibernate query: List cats = sess.createCriteria(Cat.class) .add( Restrictions.like(&quot;name&quot;, &quot;Fritz%&quot;) ) .add( Restrictions.between(&quot;weight&quot;, minWeight, maxWeight) ) .list();
  • 11. List cats = sess.createCriteria(Cat.class) .add( Restrictions.like(&quot;name&quot;, &quot;Fritz%&quot;) ) .add( Restrictions.or( Restrictions.eq( &quot;age&quot;, new Integer(0) ), Restrictions.isNull(&quot;age&quot;) ) ) .list(); List cats = sess.createCriteria(Cat.class) .add( Restrictions.in( &quot;name&quot;, new String[] { &quot;Fritz&quot;, &quot;Izi&quot;, &quot;Pk&quot; } ) ) .add( Restrictions.disjunction() .add( Restrictions.isNull(&quot;age&quot;) ) .add( Restrictions.eq(&quot;age&quot;, new Integer(0) ) ) .add( Restrictions.eq(&quot;age&quot;, new Integer(1) ) ) .add( Restrictions.eq(&quot;age&quot;, new Integer(2) ) ) ) ) .list();
  • 12. table per class hierarchy table per subclass table per concrete class
  • 13. Exactly one table is required
  • 14. Each class has its own table with the primary key related to the main class. Four tables are involved.
  • 15. Each table defines columns for all properties of the class, including inherited properties. Three tables are involved for the subclasses
  • 16. Four ways of achieving mapping the in RDBMS. One-to-one One-to-many Many-to-one Many-to-many Association can be unidirectional or bi-directional.
  • 17. One person can have only one address Table schema create table Person ( personId bigint not null primary key, addressId bigint not null unique ) create table Address ( addressId bigint not null primary key )
  • 18. Many people sharing the same address Table Schema create table Person ( personId bigint not null primary key, addressId bigint not null ) create table Address ( addressId bigint not null primary key )
  • 19. One person having many places to stay. Table Schema create table Person ( personId bigint not null primary key ) create table Address ( addressId bigint not null primary key, personId bigint not null )
  • 20. Caching can be done at various level. First level caching is done by session Second level can be done using cache frameworks Hibernate supports various implementation EHCache OSCache SwarmCache JBoss TreeCache