• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Hibernate framework-tutorial
 

Hibernate framework-tutorial

on

  • 796 views

 

Statistics

Views

Total Views
796
Views on SlideShare
796
Embed Views
0

Actions

Likes
0
Downloads
14
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Hibernate framework-tutorial Hibernate framework-tutorial Presentation Transcript

    • Snehal Shah J2EE Technology Trainer
    •  Working on Java technology since 2004.  Currently associated with Royal Technosoft Pvt. Ltd. since 2006 as a Corporate Trainer and Education Trainer.  JSP, Servlet, Struts, Spring, Hibernate and JSF and are some of the technologies I’ve been working on.
    •  Day 1 ◦ Introduction to Hibernate and its features ◦ Lab sessions  Writing entity classes and mapping metadata  Handling one-to-one, one-to-many, many-to-many and other form of associations  Day 2 ◦ ◦ ◦ ◦ Fetching strategies HQL, Query API, Criteria API Concurrency support Caching support
    • Hibernate is an ORM (Object Relational Mapping) tool/framework with a powerful API for managing persistence of Objects  Every row is represented as an instance of some class mapped to the respective table  3 primary terms on which Hibernate architecture is based on:  ◦ Transient Object ◦ Persistent Object ◦ Detached Object
    •   Any instance of a class created within the JVM process scope and valid as long as reference to the same exists Modifying the state of an object does not affects the database Object Object (state) (state) Object Object (state) (state) JV M
    • As soon as a transient object is associated with a persistence context.  Modifying the state of a persistent object has to be synchronized with the underlying database  Object Object (state) (state) Database Object Object (state) (state) Persistence Context JV M
    • As soon as a transient object is associated with a persistence context.  Modifying the state of a persistent object has to be synchronized with the underlying database  Object Object (state) (state) Object Object (state) (state) Persistence Context JV M Database
    • An object which was loaded in some persistence context but the context has been closed or maybe the transaction in which it was loaded has been committed  Modifying state of detached instance will not be updated in the database till not reattached with some persistence context 
    • Object Object (state) (state) Object Object (state) (state) JV M Persistence Context Database
    • Object Object (state) (state) Detached Detached Object Object (state) (state) JV M Database
    • Object Object (state) (state) Detached Detached Object Object (state) (state) JV M Persistence Context Database
    • EJB 3 EJB 3 Lifecycle & services managed by the container Session Bean Message Bean JPA JPA Entity Bean Persistence managed by EntityManager/ Persistence Provider Ex: Hibernate
    • Servlet/JSP Servlet/JSP page page Order Order Processing Processing EJB EJB WEB Container EJB Container Customer Customer Entity Entity Order Order Entity Entity EJB Container Persistence Provider(JPA) ex. Hibernate Database Database
    • Hibernate's goal is to relieve the developer from 95 percent of common data persistence related programming tasks, compared to manual coding with SQL and the JDBC API  Then what is left!  ◦ Handling stored procedures ◦ Handling UDT (User Defined Types) ◦ Database specific capabilities apart from this
    • What if you have 100s of tables. Lot’s of repetitive JDBC code across DAO’s  How to minimize database hits while writing JDBC code on our own  How to capitalize on performance optimization and features specific to databases  Handling exceptions, try, catch & finally is not really peace of mind 
    • Exploring Hibernate API
    • Table-per class  Table-per subclass  Table-per hierarchy 
    • Orders id name email billing_id billing_type
    • Depts depto (pk) name location Emps empno (pk) name salary deptno (fk)
    • Exploring Hibernate API
    •  Day 1 ◦ Introduction to Hibernate and its features ◦ Lab sessions  Writing entity classes and mapping metadata  Handling one-to-one, one-to-many, many-to-many and other form of associations  Day 2 ◦ ◦ ◦ ◦ Fetching strategies HQL, Query API, Criteria API Concurrency support Caching support
    •  Different ways of fetching object graph ◦ ◦ ◦ ◦ ◦ ◦ Lazy fetching Eager fetching Outer Join fetching Batch fetching Sub-select fetching Immediate fetching (HQL)
    • Exploring HQL, Criteria API, and others
    •  There are two levels of cache in Hibernate ◦ Transactional Cache  Also called as First Level Cache(FLC) implemented by Hibernate Session ◦ Global Cache  Also called as Second Level Cache(SLC) implemented by Cache providers plugged with Hibernate SessionFactory
    • Session SessionFactory Session SLC
    • Using EhCache
    • We are looking out for ways by which we can prevent concurrent updates at the same time  Locking of the row is a way by which we can easily achieve the same.  Hibernate supports both the forms of locking:  ◦ Optimistic Locking (relies on version/timestamp column) ◦ Pessimistic Locking (relies on database to manage row level locks)
    • update products set name = ?, price = ?, version = 2 where id = 99 and version = 1 Session 1 99 ID 1 99 PRICE 99 Session NAME LAPTOP 99999 VERSIO N 1
    • update products set name = ?, price = ?, version = 2 where id = 99 and version = 1 Session 2 99 ID NAME PRICE 99 LAPTOP 99999 Session 1 99 error VERSIO N 2
    • select * from products where id = 99 for update/update no wait Session 99 ID 99 PRICE 99 Session NAME LAPTOP 99999
    • Exploring optimistic and pessimistic locking