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

PPTX
Hibernate ppt
PPT
Hibernate architecture
PPT
Hibernate
PDF
Hibernate 3
PPT
Hibernate presentation
PPS
Java Hibernate Programming with Architecture Diagram and Example
PPTX
Hibernate in Action
PPTX
Hibernate Basic Concepts - Presentation
Hibernate ppt
Hibernate architecture
Hibernate
Hibernate 3
Hibernate presentation
Java Hibernate Programming with Architecture Diagram and Example
Hibernate in Action
Hibernate Basic Concepts - Presentation

What's hot (18)

PPT
Hibernate
PPTX
Hibernate
PPTX
Hibernate tutorial
PPT
Introduction to Hibernate
PDF
jsf2 Notes
PPSX
Java IO, Serialization
ODP
Hibernate Developer Reference
PPSX
Hibernate - Part 1
PDF
Hibernate Presentation
PDF
Introduction To Hibernate
PPTX
Session 35 - Design Patterns
PPSX
Struts 2 - Introduction
PPTX
Session 40 - Hibernate - Part 2
DOC
24 collections framework interview questions
PPTX
Apache Cayenne: a Java ORM Alternative
PDF
Spring db-access mod03
PPT
Persistence hibernate
PPTX
Hibernate
Hibernate
Hibernate
Hibernate tutorial
Introduction to Hibernate
jsf2 Notes
Java IO, Serialization
Hibernate Developer Reference
Hibernate - Part 1
Hibernate Presentation
Introduction To Hibernate
Session 35 - Design Patterns
Struts 2 - Introduction
Session 40 - Hibernate - Part 2
24 collections framework interview questions
Apache Cayenne: a Java ORM Alternative
Spring db-access mod03
Persistence hibernate
Hibernate
Ad

Similar to Hibernate (20)

PPTX
Module-3 for career and JFSD ppt for study.pptx
PPT
Basic Hibernate Final
PDF
hibernate-presentation-1196607644547952-4.pdf
PPT
Hibernate jj
PDF
Hibernate presentation
PPT
Hibernate introduction
PPTX
Hibernate in XPages
PDF
Free Hibernate Tutorial | VirtualNuggets
PPT
PPT
Hibernate
PPT
Patni Hibernate
PPT
2010 05-21, object-relational mapping using hibernate v2
PDF
What is hibernate?
DOCX
What is hibernate?
PPT
02 Hibernate Introduction
PPTX
Hibernate
ODP
Hibernate complete Training
DOC
Hibernate tutorial for beginners
PDF
Hibernate interview questions
PPT
Learn HIBERNATE at ASIT
Module-3 for career and JFSD ppt for study.pptx
Basic Hibernate Final
hibernate-presentation-1196607644547952-4.pdf
Hibernate jj
Hibernate presentation
Hibernate introduction
Hibernate in XPages
Free Hibernate Tutorial | VirtualNuggets
Hibernate
Patni Hibernate
2010 05-21, object-relational mapping using hibernate v2
What is hibernate?
What is hibernate?
02 Hibernate Introduction
Hibernate
Hibernate complete Training
Hibernate tutorial for beginners
Hibernate interview questions
Learn HIBERNATE at ASIT
Ad

Recently uploaded (20)

PPTX
GDM (1) (1).pptx small presentation for students
PDF
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
Yogi Goddess Pres Conference Studio Updates
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
Complications of Minimal Access Surgery at WLH
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PPTX
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
PPTX
Lesson notes of climatology university.
PDF
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
PDF
RTP_AR_KS1_Tutor's Guide_English [FOR REPRODUCTION].pdf
PPTX
Cell Types and Its function , kingdom of life
PDF
Trump Administration's workforce development strategy
PDF
Classroom Observation Tools for Teachers
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
GDM (1) (1).pptx small presentation for students
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Final Presentation General Medicine 03-08-2024.pptx
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
human mycosis Human fungal infections are called human mycosis..pptx
Yogi Goddess Pres Conference Studio Updates
O7-L3 Supply Chain Operations - ICLT Program
VCE English Exam - Section C Student Revision Booklet
Complications of Minimal Access Surgery at WLH
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
Lesson notes of climatology university.
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
RTP_AR_KS1_Tutor's Guide_English [FOR REPRODUCTION].pdf
Cell Types and Its function , kingdom of life
Trump Administration's workforce development strategy
Classroom Observation Tools for Teachers
Chinmaya Tiranga quiz Grand Finale.pdf
Chapter 2 Heredity, Prenatal Development, and Birth.pdf

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.