Your SlideShare is downloading. ×
Introduction to hibernate
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Saving this for later?

Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime - even offline.

Text the download link to your phone

Standard text messaging rates apply

Introduction to hibernate

2,317
views

Published on

Published in: Technology

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
2,317
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
115
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. By Harshit Rastogi
  • 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>
  • 6.
    • hibenrate.hbm.xml
  • 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