SlideShare a Scribd company logo
1 of 43
Harish Patil
Hibernate
Introduction
 Object – Relational Mapper(O-R mapper)
 Developed by Gavin King in 2001
 Alternative to EJB CMP 2.0
 Now part of JBoss
 Current version – 4.2.2
O-R Mapper
 Persists objects into database
 Maps java object to table and properties to column
 No need to write JDBC code
 Provides api for performing CRUD operations on objects
 Transaction Management
 E.g. Hibernate, JPA, TopLink, IBatis
Architecture
Features
 POJO entities
 Provides API for performing CRUD operations on objects
 Query API
 Transaction Management
 Rich Hibernate Query Language
 Concurrency control
 Caching
Entity
 POJO class that model the domain object
 Represents table
 Contains properties and getter & setters
 Persistent
 Annotated with @Entity
 Optionally can define @Table and @Column
Entity
Identifier
 The property that represents pk
 The property is annotated with @ID
 Id generation strategy – Auto, Identity, Sequence, Table
 Auto – Uses any of Identity, Sequence, Table generator
 Sequence – Uses sequence
 Identity – Represents identity column of mysql, sqlserver db
 Table – Uses table to store pks
SessionFactory, Session & Transaction
 Factory class for creating session
 There should be only 1 instance of SessionFactory
 Session provides api for CRUD
 Application uses session for persistence
 One session/request
 Transaction is started using session
 Transaction provides commit and rollback
SessionFactory & Session
Configuration
 Configurations are specified in hibernate.cfg.xml
 It defines –
 db connection parameters
 datasource
 transaction facory class
 entities/mapping xml
Configuration
Transaction Managers
Transaction Factory-:
 JDBCTransactionFactory
 JTATransactionFactory
Bean Managed Transaction using UserTransaction api
 CMTTransactionFactory
CMT transactions using EJBs
Lab Exercise -1
 Create Employee entity – id, name, designation
 Create EmployeeDAO – get(), add(), update(), delete()
 Create test class for dao
Entity Lifecycle & States
Relationships
 Many-To-One
Relationships
 One-To-Many
Relationships
 One-To-Many Bidirectional
Relationships
 Many-To-Many
Lazy Loading
 Load the data only when it is required
 In relationships, when a particular entity is queried its related entities
may not be required
 Lazy loading ensures that entire object graph is not created in memory
saving memory
 Lazy loading returns proxy object for related entity
 When ‘get’ is called on proxy, query is fired and all the data gets
loaded……..Session should be active
Employee Proxy
Department
Fetch Strategies
 Eager – Load all data at once
 Lazy – Lazy loading
 @ManyToOne, @OneToMany @ManyToMany has fetch
attribute to specify fetching strategy
Open Session In View Pattern
 Lazy Loading requires session to be active during view
rendering
 It is implemented using a Filter – It begins and ends
transaction and session
 Spring contains OpenSessionInViewFilter
Hibernate Query language(HQL)
 Similar to sql but uses class and property instead of table &
column
 Query class provides querying functionality
 HQL supports =,!=,<,>,In, Like, Between, And, Or
operators
 Supports group by, order by
Hibernate Query language(HQL)
 Like
 In
 Between
 And
Hibernate Query language(HQL)
 Order by
Scalar/projection Queries
 Returns scalar values rather than entities
 Specify properties to be returned after ‘select’ in query
 Returns list of objects or object arrays
Pagination
 Query supports pagination. It provides methods to set max
records to be fetched and offset
Named Query
 It is a query defined at entity level with name and query
string
 Global scope
 Defined using @NamedQuery and @NamedQueries
 Query instance is obtained using Session
Criteria API
 Query can be created dynamically using api
 Criterion represents one expression in where clause and it
is added to Criteria
 Restrictions provide methods to create Criterion
Query By Example
 Example class is a type of Criterion
 Provides simplest way to create where condition based on
the entity object
Lab Exercise
Bulk Update & Delete
 For updating/deleting multiple rows, HQL provides
update/delete query
 No need to load so many objects and then call
session.update() or session.delete()
Batching
 Call session.save() in loop
 Ensure that session is flushed & cleared frequently to
reduce memory footprint
Caching
 1st Level Cache(Session level)
Every session has its own cache
 2nd Level Cache
 It is application level cache and common to all sessions
 Cache provider needs to be configured in hibernate.cfg.xml.
hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider
 E.g. Ehcache, OSCcahe, JBoss Cache, SwarmCache
<cache name="auction.model.Category“ maxElementsInMemory="500“
eternal="true“ timeToIdleSeconds="0"
timeToLiveSeconds="0“ overflowToDisk="false“/>
Concurrency Control
Lost update:
 When two transactions are executing concurrently, 2nd transaction can overwrite
the changes made by 1st transaction
Optimistic Locking/Versioning:
 Add a property annotated by @Version
 Supported data type – int, long, Timestamp
 Add a version column in table
 Whenever entity is updated version is incremented
 If db version is not same as entity’s version then exception is thrown during
transaction commit
Concurrency Control
Concurrency Control
Pessimistic Locking:
 Session provides lock() method that explicitly locks a row
 2nd Transaction can get the entity only when 1st Transaction has
committed. This ensures that 2nd transaction has updated data
and not stale data.
 Lock mode also can be passed in get() method
Inheritance
Inheritance Strategies -:
 Table per class hierarchy(Single_Table)
 Single Table for all classes in inheritance hierarchy
 Subclasses are distinguished by Discriminator column
 @DiscriminatorColumn, @DiscriminatorValue
Inheritance
 Table per concrete class (Table_Per_Class)
 Separate table for every concrete subclass
 Every table should contain columns for superclass
properties
 Column name of super class properties can be changed
using @Override
Inheritance
 Table per sub class (Joined)
 Separate table for every superclass & subclass
 Every table should contain columns only for its own
properties and not inherited properties
 Data is retrieved by joining superclass and subclass
tables
Filters
 Hibernate provides Filter for filtering objects returned by
any query
 Define filter using @FilterDef
 Apply the filter to entity using @Filter on entity class
 Enable filter on session and set filter params
 Filter can be applied to @OneToMany relationship fields
Filters
Interceptor
 It provides callback methods
 Extend class EmptyInterceptor
 Important methods-:
 onSave()
 onLoad()
 onDelete()
 preFlush()
 postFlush()

More Related Content

What's hot

Java Persistence API (JPA) Step By Step
Java Persistence API (JPA) Step By StepJava Persistence API (JPA) Step By Step
Java Persistence API (JPA) Step By StepGuo Albert
 
Hibernate ppt
Hibernate pptHibernate ppt
Hibernate pptAneega
 
Hibernate presentation
Hibernate presentationHibernate presentation
Hibernate presentationManav Prasad
 
Introduction to JPA and Hibernate including examples
Introduction to JPA and Hibernate including examplesIntroduction to JPA and Hibernate including examples
Introduction to JPA and Hibernate including examplesecosio GmbH
 
Introduction to JPA (JPA version 2.0)
Introduction to JPA (JPA version 2.0)Introduction to JPA (JPA version 2.0)
Introduction to JPA (JPA version 2.0)ejlp12
 
jpa-hibernate-presentation
jpa-hibernate-presentationjpa-hibernate-presentation
jpa-hibernate-presentationJohn Slick
 
Hibernate inheritance and relational mappings with examples
Hibernate inheritance and relational mappings with examplesHibernate inheritance and relational mappings with examples
Hibernate inheritance and relational mappings with examplesEr. Gaurav Kumar
 
java drag and drop and data transfer
java drag and drop and data transferjava drag and drop and data transfer
java drag and drop and data transferAnkit Desai
 
Eclipse Day India 2011 - Extending JDT
Eclipse Day India 2011 - Extending JDTEclipse Day India 2011 - Extending JDT
Eclipse Day India 2011 - Extending JDTdeepakazad
 
Entity Framework: Nakov @ BFU Hackhaton 2015
Entity Framework: Nakov @ BFU Hackhaton 2015Entity Framework: Nakov @ BFU Hackhaton 2015
Entity Framework: Nakov @ BFU Hackhaton 2015Svetlin Nakov
 

What's hot (20)

Java Persistence API (JPA) Step By Step
Java Persistence API (JPA) Step By StepJava Persistence API (JPA) Step By Step
Java Persistence API (JPA) Step By Step
 
Introduction to JPA Framework
Introduction to JPA FrameworkIntroduction to JPA Framework
Introduction to JPA Framework
 
JPA Best Practices
JPA Best PracticesJPA Best Practices
JPA Best Practices
 
Hibernate ppt
Hibernate pptHibernate ppt
Hibernate ppt
 
Hibernate presentation
Hibernate presentationHibernate presentation
Hibernate presentation
 
Introduction to JPA and Hibernate including examples
Introduction to JPA and Hibernate including examplesIntroduction to JPA and Hibernate including examples
Introduction to JPA and Hibernate including examples
 
Introduction to JPA (JPA version 2.0)
Introduction to JPA (JPA version 2.0)Introduction to JPA (JPA version 2.0)
Introduction to JPA (JPA version 2.0)
 
jpa-hibernate-presentation
jpa-hibernate-presentationjpa-hibernate-presentation
jpa-hibernate-presentation
 
Jpa
JpaJpa
Jpa
 
Hibernate inheritance and relational mappings with examples
Hibernate inheritance and relational mappings with examplesHibernate inheritance and relational mappings with examples
Hibernate inheritance and relational mappings with examples
 
Java Beans
Java BeansJava Beans
Java Beans
 
Java beans
Java beansJava beans
Java beans
 
Jpa 2.1 Application Development
Jpa 2.1 Application DevelopmentJpa 2.1 Application Development
Jpa 2.1 Application Development
 
Introduction to Hibernate
Introduction to HibernateIntroduction to Hibernate
Introduction to Hibernate
 
java drag and drop and data transfer
java drag and drop and data transferjava drag and drop and data transfer
java drag and drop and data transfer
 
hibernate with JPA
hibernate with JPAhibernate with JPA
hibernate with JPA
 
Eclipse Day India 2011 - Extending JDT
Eclipse Day India 2011 - Extending JDTEclipse Day India 2011 - Extending JDT
Eclipse Day India 2011 - Extending JDT
 
JavaEE Spring Seam
JavaEE Spring SeamJavaEE Spring Seam
JavaEE Spring Seam
 
JPA and Hibernate
JPA and HibernateJPA and Hibernate
JPA and Hibernate
 
Entity Framework: Nakov @ BFU Hackhaton 2015
Entity Framework: Nakov @ BFU Hackhaton 2015Entity Framework: Nakov @ BFU Hackhaton 2015
Entity Framework: Nakov @ BFU Hackhaton 2015
 

Similar to Hibernate

02 Hibernate Introduction
02 Hibernate Introduction02 Hibernate Introduction
02 Hibernate IntroductionRanjan Kumar
 
Patni Hibernate
Patni   HibernatePatni   Hibernate
Patni Hibernatepatinijava
 
Hibernate Training Session1
Hibernate Training Session1Hibernate Training Session1
Hibernate Training Session1Asad Khan
 
Java Web Programming on Google Cloud Platform [2/3] : Datastore
Java Web Programming on Google Cloud Platform [2/3] : DatastoreJava Web Programming on Google Cloud Platform [2/3] : Datastore
Java Web Programming on Google Cloud Platform [2/3] : DatastoreIMC Institute
 
ADF Gold Nuggets (Oracle Open World 2011)
ADF Gold Nuggets (Oracle Open World 2011)ADF Gold Nuggets (Oracle Open World 2011)
ADF Gold Nuggets (Oracle Open World 2011)Lucas Jellema
 
Advanced Hibernate V2
Advanced Hibernate V2Advanced Hibernate V2
Advanced Hibernate V2Haitham Raik
 
Accelerated data access
Accelerated data accessAccelerated data access
Accelerated data accessgordonyorke
 
Session 6 Tp6
Session 6 Tp6Session 6 Tp6
Session 6 Tp6phanleson
 
Introduction to c_plus_plus
Introduction to c_plus_plusIntroduction to c_plus_plus
Introduction to c_plus_plusSayed Ahmed
 
Introduction to c_plus_plus (6)
Introduction to c_plus_plus (6)Introduction to c_plus_plus (6)
Introduction to c_plus_plus (6)Sayed Ahmed
 
Session 5 Tp5
Session 5 Tp5Session 5 Tp5
Session 5 Tp5phanleson
 
02 java spring-hibernate-experience-questions
02 java spring-hibernate-experience-questions02 java spring-hibernate-experience-questions
02 java spring-hibernate-experience-questionsDhiraj Champawat
 
Object Oriented Programming In .Net
Object Oriented Programming In .NetObject Oriented Programming In .Net
Object Oriented Programming In .NetGreg Sohl
 

Similar to Hibernate (20)

Hibernate
HibernateHibernate
Hibernate
 
Ejb3 Presentation
Ejb3 PresentationEjb3 Presentation
Ejb3 Presentation
 
Lift Framework
Lift FrameworkLift Framework
Lift Framework
 
02 Hibernate Introduction
02 Hibernate Introduction02 Hibernate Introduction
02 Hibernate Introduction
 
Hibernate
HibernateHibernate
Hibernate
 
Patni Hibernate
Patni   HibernatePatni   Hibernate
Patni Hibernate
 
Hibernate Training Session1
Hibernate Training Session1Hibernate Training Session1
Hibernate Training Session1
 
Introduction to Datastore
Introduction to DatastoreIntroduction to Datastore
Introduction to Datastore
 
Java Web Programming on Google Cloud Platform [2/3] : Datastore
Java Web Programming on Google Cloud Platform [2/3] : DatastoreJava Web Programming on Google Cloud Platform [2/3] : Datastore
Java Web Programming on Google Cloud Platform [2/3] : Datastore
 
Jpa
JpaJpa
Jpa
 
Hibernate 3
Hibernate 3Hibernate 3
Hibernate 3
 
ADF Gold Nuggets (Oracle Open World 2011)
ADF Gold Nuggets (Oracle Open World 2011)ADF Gold Nuggets (Oracle Open World 2011)
ADF Gold Nuggets (Oracle Open World 2011)
 
Advanced Hibernate V2
Advanced Hibernate V2Advanced Hibernate V2
Advanced Hibernate V2
 
Accelerated data access
Accelerated data accessAccelerated data access
Accelerated data access
 
Session 6 Tp6
Session 6 Tp6Session 6 Tp6
Session 6 Tp6
 
Introduction to c_plus_plus
Introduction to c_plus_plusIntroduction to c_plus_plus
Introduction to c_plus_plus
 
Introduction to c_plus_plus (6)
Introduction to c_plus_plus (6)Introduction to c_plus_plus (6)
Introduction to c_plus_plus (6)
 
Session 5 Tp5
Session 5 Tp5Session 5 Tp5
Session 5 Tp5
 
02 java spring-hibernate-experience-questions
02 java spring-hibernate-experience-questions02 java spring-hibernate-experience-questions
02 java spring-hibernate-experience-questions
 
Object Oriented Programming In .Net
Object Oriented Programming In .NetObject Oriented Programming In .Net
Object Oriented Programming In .Net
 

Recently uploaded

%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...masabamasaba
 
%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Hararemasabamasaba
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park masabamasaba
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsJhone kinadey
 
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...Shane Coughlan
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️Delhi Call girls
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...masabamasaba
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfonteinmasabamasaba
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrandmasabamasaba
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnAmarnathKambale
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech studentsHimanshiGarg82
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in sowetomasabamasaba
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...masabamasaba
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...Health
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...SelfMade bd
 
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisamasabamasaba
 

Recently uploaded (20)

%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
 
%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
 

Hibernate

  • 2. Introduction  Object – Relational Mapper(O-R mapper)  Developed by Gavin King in 2001  Alternative to EJB CMP 2.0  Now part of JBoss  Current version – 4.2.2
  • 3. O-R Mapper  Persists objects into database  Maps java object to table and properties to column  No need to write JDBC code  Provides api for performing CRUD operations on objects  Transaction Management  E.g. Hibernate, JPA, TopLink, IBatis
  • 5. Features  POJO entities  Provides API for performing CRUD operations on objects  Query API  Transaction Management  Rich Hibernate Query Language  Concurrency control  Caching
  • 6. Entity  POJO class that model the domain object  Represents table  Contains properties and getter & setters  Persistent  Annotated with @Entity  Optionally can define @Table and @Column
  • 8. Identifier  The property that represents pk  The property is annotated with @ID  Id generation strategy – Auto, Identity, Sequence, Table  Auto – Uses any of Identity, Sequence, Table generator  Sequence – Uses sequence  Identity – Represents identity column of mysql, sqlserver db  Table – Uses table to store pks
  • 9. SessionFactory, Session & Transaction  Factory class for creating session  There should be only 1 instance of SessionFactory  Session provides api for CRUD  Application uses session for persistence  One session/request  Transaction is started using session  Transaction provides commit and rollback
  • 11. Configuration  Configurations are specified in hibernate.cfg.xml  It defines –  db connection parameters  datasource  transaction facory class  entities/mapping xml
  • 13. Transaction Managers Transaction Factory-:  JDBCTransactionFactory  JTATransactionFactory Bean Managed Transaction using UserTransaction api  CMTTransactionFactory CMT transactions using EJBs
  • 14. Lab Exercise -1  Create Employee entity – id, name, designation  Create EmployeeDAO – get(), add(), update(), delete()  Create test class for dao
  • 20. Lazy Loading  Load the data only when it is required  In relationships, when a particular entity is queried its related entities may not be required  Lazy loading ensures that entire object graph is not created in memory saving memory  Lazy loading returns proxy object for related entity  When ‘get’ is called on proxy, query is fired and all the data gets loaded……..Session should be active Employee Proxy Department
  • 21. Fetch Strategies  Eager – Load all data at once  Lazy – Lazy loading  @ManyToOne, @OneToMany @ManyToMany has fetch attribute to specify fetching strategy
  • 22. Open Session In View Pattern  Lazy Loading requires session to be active during view rendering  It is implemented using a Filter – It begins and ends transaction and session  Spring contains OpenSessionInViewFilter
  • 23. Hibernate Query language(HQL)  Similar to sql but uses class and property instead of table & column  Query class provides querying functionality  HQL supports =,!=,<,>,In, Like, Between, And, Or operators  Supports group by, order by
  • 24. Hibernate Query language(HQL)  Like  In  Between  And
  • 26. Scalar/projection Queries  Returns scalar values rather than entities  Specify properties to be returned after ‘select’ in query  Returns list of objects or object arrays
  • 27. Pagination  Query supports pagination. It provides methods to set max records to be fetched and offset
  • 28. Named Query  It is a query defined at entity level with name and query string  Global scope  Defined using @NamedQuery and @NamedQueries  Query instance is obtained using Session
  • 29. Criteria API  Query can be created dynamically using api  Criterion represents one expression in where clause and it is added to Criteria  Restrictions provide methods to create Criterion
  • 30. Query By Example  Example class is a type of Criterion  Provides simplest way to create where condition based on the entity object
  • 32. Bulk Update & Delete  For updating/deleting multiple rows, HQL provides update/delete query  No need to load so many objects and then call session.update() or session.delete()
  • 33. Batching  Call session.save() in loop  Ensure that session is flushed & cleared frequently to reduce memory footprint
  • 34. Caching  1st Level Cache(Session level) Every session has its own cache  2nd Level Cache  It is application level cache and common to all sessions  Cache provider needs to be configured in hibernate.cfg.xml. hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider  E.g. Ehcache, OSCcahe, JBoss Cache, SwarmCache <cache name="auction.model.Category“ maxElementsInMemory="500“ eternal="true“ timeToIdleSeconds="0" timeToLiveSeconds="0“ overflowToDisk="false“/>
  • 35. Concurrency Control Lost update:  When two transactions are executing concurrently, 2nd transaction can overwrite the changes made by 1st transaction Optimistic Locking/Versioning:  Add a property annotated by @Version  Supported data type – int, long, Timestamp  Add a version column in table  Whenever entity is updated version is incremented  If db version is not same as entity’s version then exception is thrown during transaction commit
  • 37. Concurrency Control Pessimistic Locking:  Session provides lock() method that explicitly locks a row  2nd Transaction can get the entity only when 1st Transaction has committed. This ensures that 2nd transaction has updated data and not stale data.  Lock mode also can be passed in get() method
  • 38. Inheritance Inheritance Strategies -:  Table per class hierarchy(Single_Table)  Single Table for all classes in inheritance hierarchy  Subclasses are distinguished by Discriminator column  @DiscriminatorColumn, @DiscriminatorValue
  • 39. Inheritance  Table per concrete class (Table_Per_Class)  Separate table for every concrete subclass  Every table should contain columns for superclass properties  Column name of super class properties can be changed using @Override
  • 40. Inheritance  Table per sub class (Joined)  Separate table for every superclass & subclass  Every table should contain columns only for its own properties and not inherited properties  Data is retrieved by joining superclass and subclass tables
  • 41. Filters  Hibernate provides Filter for filtering objects returned by any query  Define filter using @FilterDef  Apply the filter to entity using @Filter on entity class  Enable filter on session and set filter params  Filter can be applied to @OneToMany relationship fields
  • 43. Interceptor  It provides callback methods  Extend class EmptyInterceptor  Important methods-:  onSave()  onLoad()  onDelete()  preFlush()  postFlush()