SlideShare a Scribd company logo
CONTENTS
1. Overview
2. Advantages over JDBC
3. Hibernate Architecture
4. Downloading and Installing Hibernate
Overview
• A popular open source Object Relational Mapping(ORM) tool which
provides transparent persistence for POJOS.
• ORM is used to map objects to relational databases(Oracle,Mysql).
• Allows to map java objects to relational databases and also provides data
query
• Generates the SQL calls and makes application portable to all SQL
databases, with database portability
• Allows to develop persistent classes with all java terms – like association,
inheritance, polymorphism etc
• Can be used as in standalone Java applications or as in Java EE
applications using servlets or EJB session beans.
Advantages of Hibernate
• Retains natural object model (transparent)
• Minimizes Code
• Does not require a container
• Model is not tied to persistence implementation.
• Metadata controlled persistence
• Transparent - working with the model, not the data access technology
• Pooling, Caching, Transactions can be handled outside of our code
• Object/relational mappings are defined in an XML document. The
mapping document is designed to be readable and hand-editable.
• Mapping language is Java-centric, meaning that mappings are
constructed around persistent class declarations, not table declarations.
• Choose to define XML mappings by hand, or by using tools like XDoclet,
Middlegen and AndroMDA.
Hibernate
Hibernate
• The “Lite" architecture has the application provide its own
JDBC connections and manage its own transactions
• Has
– SessionFactory
– Session
– Persistent Objects
– Transient Objects
• The "full cream" architecture abstracts the application away
from the underlying JDBC and Java Transaction API (JTA) and
lets Hibernate take care of the details
Hibernate
Configuration
Configuration class's operation has two key components:
• The database connection
– handled through one or more configuration files supported by
Hibernate
– hibernate.properties and hibernate.cfg.xml
• The classmapping setup
– makes the connection between the Java classes and database tables.
hibernate.cfg.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-
2.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.driver">
com.mysql.jdbc.Driver </property>
<property name="connection.url“>
jdbc:mysql://localhost/products </property>
<property name="dialect">
net.sf.hibernate.dialect.MySQLDialect </property>
</session-factory>
</hibernate-configuration>
hibernate.properties
hibernate.dialect net.sf.hibernate.dialect.MySQLDialect
hibernate.connection.driver_class com.mysql.jdbc.Driver
hibernate.connection.url jdbc:mysql://localhost/products
hibernate.connection.username root
hibernate.connection.password root
hibernate.show_sql true
hibernate.cglib.use_reflection_optimizer false
Hibernate mapping file
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="Notes“ table="notes">
<id name="id" unsaved-value="0">
<generator class="native"/></id>
<property name="info" type="string"/>
<property name="count" type="integer" not-null="true"/>
<property name="zipcode" type="string"/>
<property name="fullname" type="string"/>
</class></hibernate-mapping>
Hibernate Architecture
• Basic interfaces used while working with Hibernate are:
- SessionFactory
- Session
- Transaction
SessionFactory
• A singleton class called SessionFactory is instantiated.
• A threadsafe, immutable cache of compiled mappings for a single
database.
• The SessionFactory object is heavyweight; better to create only one
SessionFactory per application.
• A factory for creating session objects
• Can hold an optional(second-level) cache of data that is reusable between
transactions at a process
Session
• A single-threaded, short-lived object representing a conversation between
the application and the persistent store.
• is lightweight and instantiated each time an interaction is needed with
the database.
• As the Session object is created, the objects are persisted or loaded from
the database
• Then the session object is flushed and closed.
• It wraps a JDBC connection and is a factory for Transaction.
• Opened by SessionFactory.openSession()
• Closed by session.close()
ThreadLocal Session Pattern
• Use ThreadLocal variable to maintain session data
ThreadLocal
• Specialized Java class which is associated to a user thread of execution and
serves as a data container throughout the lifespan of the thread (or child
threads)
• Global variable which is local for every thread
• To create a thread
ThreadLocal sess = new ThreadLocal();
Session session = sess.get(); // to get the current session
Transaction
• A single-threaded, short-lived object used by the application
to specify atomic units of work.
• It abstracts the application from the underlying JDBC, JTA or
CORBA transaction.
• A Session might span several Transactions in some cases. is
connected with a concrete session
Transaction tx = session.beginTransaction( );
Persistent objects and collections(Entities)
• Short-lived, single threaded objects containing persistent
state and business function.
• Can be ordinary JavaBeans/POJOs.
• associated with exactly one Session.
Transient and detached objects and collections
• Instances of persistent classes that are not currently
associated with a Session.
• Might have been instantiated by the application and not yet
persisted
• Might have been instantiated by a closed Session.
Hibernate Callbacks
• public Boolean onSave(Session s); - called before the object is saved to the
• database
• public Boolean onUpdate(Session s); - called before the object is updated
• public boolean onDelete(Session s); - called before the object is deleted
• public Boolean onLoad(Session s); - called after the object is loaded from
• the database
Downloading and Installing
Downloading
• Download Hibernate from the site (ie) from JBoss tools as Zip
• Extract the files to your preferred location
• Has 2 folders – plugins and features
Adding to Eclipse
• Add the files within hibernate /features folder to
eclipse/features folder
• Add the files within hibernate /plugins folder to
eclipse/plugins folder
Alternate Way
• Open eclipse Help  Update site add hibernate from site

More Related Content

What's hot (18)

PPT
Hibernate
Ajay K
 
PPTX
Hibernate
Prashant Kalkar
 
PPTX
Hibernate tutorial
Mumbai Academisc
 
PPT
Introduction to Hibernate
Krishnakanth Goud
 
PDF
jsf2 Notes
Rajiv Gupta
 
PPSX
Java IO, Serialization
Hitesh-Java
 
ODP
Hibernate Developer Reference
Muthuselvam RS
 
PPSX
Hibernate - Part 1
Hitesh-Java
 
PDF
Hibernate Presentation
guest11106b
 
PDF
Introduction To Hibernate
ashishkulkarni
 
PPTX
Session 35 - Design Patterns
PawanMM
 
PPSX
Struts 2 - Introduction
Hitesh-Java
 
PPTX
Session 40 - Hibernate - Part 2
PawanMM
 
DOC
24 collections framework interview questions
Arun Vasanth
 
PPTX
Apache Cayenne: a Java ORM Alternative
Andrus Adamchik
 
PDF
Spring db-access mod03
Guo Albert
 
PPT
Persistence hibernate
Krishnakanth Goud
 
PPTX
Hibernate
Sujit Kumar
 
Hibernate
Ajay K
 
Hibernate
Prashant Kalkar
 
Hibernate tutorial
Mumbai Academisc
 
Introduction to Hibernate
Krishnakanth Goud
 
jsf2 Notes
Rajiv Gupta
 
Java IO, Serialization
Hitesh-Java
 
Hibernate Developer Reference
Muthuselvam RS
 
Hibernate - Part 1
Hitesh-Java
 
Hibernate Presentation
guest11106b
 
Introduction To Hibernate
ashishkulkarni
 
Session 35 - Design Patterns
PawanMM
 
Struts 2 - Introduction
Hitesh-Java
 
Session 40 - Hibernate - Part 2
PawanMM
 
24 collections framework interview questions
Arun Vasanth
 
Apache Cayenne: a Java ORM Alternative
Andrus Adamchik
 
Spring db-access mod03
Guo Albert
 
Persistence hibernate
Krishnakanth Goud
 
Hibernate
Sujit Kumar
 

Similar to Hibernate (20)

PPTX
Module-3 for career and JFSD ppt for study.pptx
ViratKohli78
 
PPT
Basic Hibernate Final
Rafael Coutinho
 
PDF
hibernate-presentation-1196607644547952-4.pdf
Mytrux1
 
PPT
Hibernate jj
Joe Jacob
 
PDF
Hibernate presentation
Luis Goldster
 
PPT
Hibernate introduction
Sagar Verma
 
PPTX
Hibernate in XPages
Toby Samples
 
PDF
Free Hibernate Tutorial | VirtualNuggets
Virtual Nuggets
 
PPT
Hibernate
Murali Pachiyappan
 
PPT
Hibernate
Shaharyar khan
 
PPT
Patni Hibernate
patinijava
 
PPT
2010 05-21, object-relational mapping using hibernate v2
alvaro alcocer sotil
 
PDF
What is hibernate?
kanchanmahajan23
 
DOCX
What is hibernate?
kanchanmahajan23
 
PPT
02 Hibernate Introduction
Ranjan Kumar
 
PPTX
Hibernate
Mallikarjuna G D
 
ODP
Hibernate complete Training
sourabh aggarwal
 
DOC
Hibernate tutorial for beginners
Rahul Jain
 
PDF
Hibernate interview questions
venkata52
 
PPT
Learn HIBERNATE at ASIT
ASIT
 
Module-3 for career and JFSD ppt for study.pptx
ViratKohli78
 
Basic Hibernate Final
Rafael Coutinho
 
hibernate-presentation-1196607644547952-4.pdf
Mytrux1
 
Hibernate jj
Joe Jacob
 
Hibernate presentation
Luis Goldster
 
Hibernate introduction
Sagar Verma
 
Hibernate in XPages
Toby Samples
 
Free Hibernate Tutorial | VirtualNuggets
Virtual Nuggets
 
Hibernate
Shaharyar khan
 
Patni Hibernate
patinijava
 
2010 05-21, object-relational mapping using hibernate v2
alvaro alcocer sotil
 
What is hibernate?
kanchanmahajan23
 
What is hibernate?
kanchanmahajan23
 
02 Hibernate Introduction
Ranjan Kumar
 
Hibernate
Mallikarjuna G D
 
Hibernate complete Training
sourabh aggarwal
 
Hibernate tutorial for beginners
Rahul Jain
 
Hibernate interview questions
venkata52
 
Learn HIBERNATE at ASIT
ASIT
 
Ad

Recently uploaded (20)

DOCX
Modul Ajar Deep Learning Bahasa Inggris Kelas 11 Terbaru 2025
wahyurestu63
 
PPTX
Continental Accounting in Odoo 18 - Odoo Slides
Celine George
 
PPTX
I INCLUDED THIS TOPIC IS INTELLIGENCE DEFINITION, MEANING, INDIVIDUAL DIFFERE...
parmarjuli1412
 
PPTX
PROTIEN ENERGY MALNUTRITION: NURSING MANAGEMENT.pptx
PRADEEP ABOTHU
 
PPTX
IDEAS AND EARLY STATES Social science pptx
NIRANJANASSURESH
 
PPTX
20250924 Navigating the Future: How to tell the difference between an emergen...
McGuinness Institute
 
PPTX
Virus sequence retrieval from NCBI database
yamunaK13
 
PPTX
Artificial Intelligence in Gastroentrology: Advancements and Future Presprec...
AyanHossain
 
DOCX
pgdei-UNIT -V Neurological Disorders & developmental disabilities
JELLA VISHNU DURGA PRASAD
 
PPTX
Rules and Regulations of Madhya Pradesh Library Part-I
SantoshKumarKori2
 
PDF
My Thoughts On Q&A- A Novel By Vikas Swarup
Niharika
 
PDF
John Keats introduction and list of his important works
vatsalacpr
 
PPTX
Command Palatte in Odoo 18.1 Spreadsheet - Odoo Slides
Celine George
 
PPTX
Constitutional Design Civics Class 9.pptx
bikesh692
 
PPTX
Cleaning Validation Ppt Pharmaceutical validation
Ms. Ashatai Patil
 
PPTX
Applications of matrices In Real Life_20250724_091307_0000.pptx
gehlotkrish03
 
PPTX
Sonnet 130_ My Mistress’ Eyes Are Nothing Like the Sun By William Shakespear...
DhatriParmar
 
PPTX
CONCEPT OF CHILD CARE. pptx
AneetaSharma15
 
PDF
Stepwise procedure (Manually Submitted & Un Attended) Medical Devices Cases
MUHAMMAD SOHAIL
 
PDF
TOP 10 AI TOOLS YOU MUST LEARN TO SURVIVE IN 2025 AND ABOVE
digilearnings.com
 
Modul Ajar Deep Learning Bahasa Inggris Kelas 11 Terbaru 2025
wahyurestu63
 
Continental Accounting in Odoo 18 - Odoo Slides
Celine George
 
I INCLUDED THIS TOPIC IS INTELLIGENCE DEFINITION, MEANING, INDIVIDUAL DIFFERE...
parmarjuli1412
 
PROTIEN ENERGY MALNUTRITION: NURSING MANAGEMENT.pptx
PRADEEP ABOTHU
 
IDEAS AND EARLY STATES Social science pptx
NIRANJANASSURESH
 
20250924 Navigating the Future: How to tell the difference between an emergen...
McGuinness Institute
 
Virus sequence retrieval from NCBI database
yamunaK13
 
Artificial Intelligence in Gastroentrology: Advancements and Future Presprec...
AyanHossain
 
pgdei-UNIT -V Neurological Disorders & developmental disabilities
JELLA VISHNU DURGA PRASAD
 
Rules and Regulations of Madhya Pradesh Library Part-I
SantoshKumarKori2
 
My Thoughts On Q&A- A Novel By Vikas Swarup
Niharika
 
John Keats introduction and list of his important works
vatsalacpr
 
Command Palatte in Odoo 18.1 Spreadsheet - Odoo Slides
Celine George
 
Constitutional Design Civics Class 9.pptx
bikesh692
 
Cleaning Validation Ppt Pharmaceutical validation
Ms. Ashatai Patil
 
Applications of matrices In Real Life_20250724_091307_0000.pptx
gehlotkrish03
 
Sonnet 130_ My Mistress’ Eyes Are Nothing Like the Sun By William Shakespear...
DhatriParmar
 
CONCEPT OF CHILD CARE. pptx
AneetaSharma15
 
Stepwise procedure (Manually Submitted & Un Attended) Medical Devices Cases
MUHAMMAD SOHAIL
 
TOP 10 AI TOOLS YOU MUST LEARN TO SURVIVE IN 2025 AND ABOVE
digilearnings.com
 
Ad

Hibernate

  • 1. CONTENTS 1. Overview 2. Advantages over JDBC 3. Hibernate Architecture 4. Downloading and Installing Hibernate
  • 2. Overview • A popular open source Object Relational Mapping(ORM) tool which provides transparent persistence for POJOS. • ORM is used to map objects to relational databases(Oracle,Mysql). • Allows to map java objects to relational databases and also provides data query • Generates the SQL calls and makes application portable to all SQL databases, with database portability • Allows to develop persistent classes with all java terms – like association, inheritance, polymorphism etc • Can be used as in standalone Java applications or as in Java EE applications using servlets or EJB session beans.
  • 3. Advantages of Hibernate • Retains natural object model (transparent) • Minimizes Code • Does not require a container • Model is not tied to persistence implementation. • Metadata controlled persistence • Transparent - working with the model, not the data access technology • Pooling, Caching, Transactions can be handled outside of our code • Object/relational mappings are defined in an XML document. The mapping document is designed to be readable and hand-editable. • Mapping language is Java-centric, meaning that mappings are constructed around persistent class declarations, not table declarations. • Choose to define XML mappings by hand, or by using tools like XDoclet, Middlegen and AndroMDA.
  • 6. • The “Lite" architecture has the application provide its own JDBC connections and manage its own transactions • Has – SessionFactory – Session – Persistent Objects – Transient Objects • The "full cream" architecture abstracts the application away from the underlying JDBC and Java Transaction API (JTA) and lets Hibernate take care of the details
  • 8. Configuration Configuration class's operation has two key components: • The database connection – handled through one or more configuration files supported by Hibernate – hibernate.properties and hibernate.cfg.xml • The classmapping setup – makes the connection between the Java classes and database tables.
  • 9. hibernate.cfg.xml <?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN" "http://hibernate.sourceforge.net/hibernate-configuration- 2.0.dtd"> <hibernate-configuration> <session-factory> <property name="connection.driver"> com.mysql.jdbc.Driver </property> <property name="connection.url“> jdbc:mysql://localhost/products </property> <property name="dialect"> net.sf.hibernate.dialect.MySQLDialect </property> </session-factory> </hibernate-configuration>
  • 10. hibernate.properties hibernate.dialect net.sf.hibernate.dialect.MySQLDialect hibernate.connection.driver_class com.mysql.jdbc.Driver hibernate.connection.url jdbc:mysql://localhost/products hibernate.connection.username root hibernate.connection.password root hibernate.show_sql true hibernate.cglib.use_reflection_optimizer false
  • 11. Hibernate mapping file <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd"> <hibernate-mapping> <class name="Notes“ table="notes"> <id name="id" unsaved-value="0"> <generator class="native"/></id> <property name="info" type="string"/> <property name="count" type="integer" not-null="true"/> <property name="zipcode" type="string"/> <property name="fullname" type="string"/> </class></hibernate-mapping>
  • 12. Hibernate Architecture • Basic interfaces used while working with Hibernate are: - SessionFactory - Session - Transaction
  • 13. SessionFactory • A singleton class called SessionFactory is instantiated. • A threadsafe, immutable cache of compiled mappings for a single database. • The SessionFactory object is heavyweight; better to create only one SessionFactory per application. • A factory for creating session objects • Can hold an optional(second-level) cache of data that is reusable between transactions at a process
  • 14. Session • A single-threaded, short-lived object representing a conversation between the application and the persistent store. • is lightweight and instantiated each time an interaction is needed with the database. • As the Session object is created, the objects are persisted or loaded from the database • Then the session object is flushed and closed. • It wraps a JDBC connection and is a factory for Transaction. • Opened by SessionFactory.openSession() • Closed by session.close()
  • 15. ThreadLocal Session Pattern • Use ThreadLocal variable to maintain session data ThreadLocal • Specialized Java class which is associated to a user thread of execution and serves as a data container throughout the lifespan of the thread (or child threads) • Global variable which is local for every thread • To create a thread ThreadLocal sess = new ThreadLocal(); Session session = sess.get(); // to get the current session
  • 16. Transaction • A single-threaded, short-lived object used by the application to specify atomic units of work. • It abstracts the application from the underlying JDBC, JTA or CORBA transaction. • A Session might span several Transactions in some cases. is connected with a concrete session Transaction tx = session.beginTransaction( );
  • 17. Persistent objects and collections(Entities) • Short-lived, single threaded objects containing persistent state and business function. • Can be ordinary JavaBeans/POJOs. • associated with exactly one Session. Transient and detached objects and collections • Instances of persistent classes that are not currently associated with a Session. • Might have been instantiated by the application and not yet persisted • Might have been instantiated by a closed Session.
  • 18. Hibernate Callbacks • public Boolean onSave(Session s); - called before the object is saved to the • database • public Boolean onUpdate(Session s); - called before the object is updated • public boolean onDelete(Session s); - called before the object is deleted • public Boolean onLoad(Session s); - called after the object is loaded from • the database
  • 19. Downloading and Installing Downloading • Download Hibernate from the site (ie) from JBoss tools as Zip • Extract the files to your preferred location • Has 2 folders – plugins and features Adding to Eclipse • Add the files within hibernate /features folder to eclipse/features folder • Add the files within hibernate /plugins folder to eclipse/plugins folder Alternate Way • Open eclipse Help  Update site add hibernate from site

Editor's Notes

  • #4: Dirty checking Hibernate allows dirty checking feature.All persistent objects are monitored by hibernate.it detects which objects have been modified and then calls update statements on all updated objects.the process of updating the changed object is called automatic dirty checking. Dirty checking allows the user or developer to avoid the time consuming databases write actions. This feature makes necessary updations and changes to the fields which require a change, remaining fields are left unchanged or untouched.