Introduction to hibernate

5,487 views

Published on

Published in: Technology
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
5,487
On SlideShare
0
From Embeds
0
Number of Embeds
11
Actions
Shares
0
Downloads
276
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

Introduction to hibernate

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

×