SlideShare a Scribd company logo
Hibernate
Content covered
• Introduction to Hibernate
• Hibernate Architecture
• O/R Mapping in Hibernate
Hibernate Architecture
Configuration
(org.hibernate.cfg.Configuration)
• The Configuration object, as seen in the
example code, is the first Hibernate object you
use. It represents a configuration or properties
file for Hibernate. The Configuration object is
usually created once during application
initialization.
• The Configuration object reads and establishes
the properties Hibernate uses to get connected
to a database and configure itself for work. A
Configuration object is used to create a
SessionFactory and then typically is discarded.
Example
<?xml version="1.0" encoding="UTF-8"?>
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class"> org.hsqldb.jdbcDriver
</property>
<property name="hibernate.connection.url">jdbc:hsqldb:hsql://localhost
</property>
<property name="hibernate.connection.username">sa</property>
<property name="connection.password"></property>
<property name="connection.pool_size">1</property>
<property name="hibernate.dialect"> org.hibernate.dialect.HSQLDialect
</property>
<mapping resource="com/vaannila/student/Student.hbm.xml"/>
<mapping resource="com/vaannila/student/Address.hbm.xml"/>
</session-factory>
</hibernate-configuration>
SessionFactory
(org.hibernate.SessionFactory)
• Created from a Configuration object, and
as its name implies it is a factory for
Session objects.
• An expensive object to create. It, like the
Configuration object, is usually created
during application start up. However,
unlike the Configuration object, It should
be created once and kept for later use.
SessionFactory
• SessionFactory object,
– used by all the threads of an application. It is
a thread safe object.
– One SessionFactory object is created per
database.
– Multiple SessionFactory objects (each
requiring a separate Configuration) are
created when connecting to multiple
databases.
Hibernate Flow of Work
Session
(org.hibernate.Session)
• Provide the main interface to accomplish work with the
database. Persistent objects are saved
and retrieved through a Session object.
• Lightweight & inexpensive to create, does the work of
getting a physical connection to the database.
• Not thread safe. Therefore, session objects should not
be kept open for a long time.
• Applications create and destroy these as needed.
• When modifications are made to the database, Session
objects create a Transaction object.
Transaction
(org.hibernate.Transaction)
• Represents a unit of work with the database
(and potentially other systems). Transactions in
Hibernate are handled by an underlying
transaction manager and transaction (from
JDBC, JTA or CORBA).
• Abstracts the developer from having to deal with
the underlying transaction manager/transaction.
• Transaction objects should be kept open for as
short of time as
possible.
Example
Sessionsession=
SessionFactory.newSession();
Transaction tx = session.beginTransaction( );
tx.commit(); tx.rollback();
session.close()
Query & Criteria
• Both objects are used to retrieve (and
recreate) persistent objects.
• Query objects use SQL or Hibernate
Query Language (HQL) string to retrieve
data from the database & create objects.
• Criteria enables developer to create and
execute the object-oriented criteria
queries.
Hibernate O/R Mapping
• ORM is a technique to map object-
oriented data with the relational data.
• Used to convert datatype supported in
object-oriented programming language to
a datatype to a data type supported by a
database. (if mapping is not provided,
exception is thrown while transferring an
object oriented data to the database)
Hibernate O/R Mapping
• Mapping in the format that relate
– Classes with tables.
– Properties with columns
– SQL types with Java types.
• O/R Mapping can be written for any
database that can be oracle, DB2, MySql,
Sybase.
Hibernate O/R Mapping
• xml file
XML mapping documents allows you to:
– Bridge the gap between object-oriented
systems and relational databases
– Define the object-relational mapping
– Help to generate and export database table
Associations strategies
• Mainly 3 Association strategies available
in O/R mapping:
• 1. One-2-one
• 2. One-2-many
• 3. Many-2-one
• 4. Many-2-many
Hibernate Mapping files
<?xml version="1.0"?>
<hibernate-mapping>
<class name="com.vaannila.student.Student" table="STUDENT">
<id name="studentId" type="long" column="STUDENT_ID">
<generator class="native“/></id>
<property name="studentName" type="string" not-null="true"
length="100" column="STUDENT_NAME" />
<many-to-one name="studentAddress"
class="com.vaannila.student.Address"
column="STUDENT_ADDRESS" not-null="true" cascade="all"
unique="true" />
</class>
</hibernate-mapping>

More Related Content

PPT
Hibernate jj
PPT
Java hibernate orm implementation tool
PPT
Hibernate Session 1
PDF
Cassandra data access
PPT
Learn HIBERNATE at ASIT
PPT
Introduction to NHibernate
PPT
Core Data Migration
PPTX
NHibernate for .NET
Hibernate jj
Java hibernate orm implementation tool
Hibernate Session 1
Cassandra data access
Learn HIBERNATE at ASIT
Introduction to NHibernate
Core Data Migration
NHibernate for .NET

What's hot (20)

PDF
Electron, databases, and RxDB
PPTX
Introduction to Hibernate Framework
ODP
PPTX
Spring Data - Intro (Odessa Java TechTalks)
PDF
NHibernate (The ORM For .NET Platform)
PPTX
Kantara OTTO slides
PPT
DataFinder concepts and example: General (20100503)
PPTX
NHibernate
PPT
Introduction to hibernate
PPTX
ELK - Stack - Munich .net UG
PDF
Deep dive into the native multi model database ArangoDB
PPTX
Configuring jpa in a Spring application
PDF
MySQL Connector/Node.js and the X DevAPI
ODP
Indexed db
PPTX
Elasticsearch an overview
PDF
Node.js and the MySQL Document Store
PPTX
IMC Summit 2016 Breakout - William Bain - Implementing Extensible Data Struct...
PDF
Webinar Slides: Tungsten Replicator for Elasticsearch - Real-time data loadin...
PDF
Quick overview on mongo db
PPTX
Elastic Stack Introduction
Electron, databases, and RxDB
Introduction to Hibernate Framework
Spring Data - Intro (Odessa Java TechTalks)
NHibernate (The ORM For .NET Platform)
Kantara OTTO slides
DataFinder concepts and example: General (20100503)
NHibernate
Introduction to hibernate
ELK - Stack - Munich .net UG
Deep dive into the native multi model database ArangoDB
Configuring jpa in a Spring application
MySQL Connector/Node.js and the X DevAPI
Indexed db
Elasticsearch an overview
Node.js and the MySQL Document Store
IMC Summit 2016 Breakout - William Bain - Implementing Extensible Data Struct...
Webinar Slides: Tungsten Replicator for Elasticsearch - Real-time data loadin...
Quick overview on mongo db
Elastic Stack Introduction
Ad

Viewers also liked (18)

PDF
The Magic of ATMvertising
PDF
Austin Web Architecture
PPTX
Radcab
PPT
Презентация
PPT
This Is How I See 2012 Results To Date
PDF
Prasanth1
PDF
Happy DevOps with Apache Cassandra and DataStax
PPT
ысоцкий
PPT
This Is How I See 2012 Results To Date231211
PDF
HFA pMDI Patents Perspectives Gold Mine Or Minefield Ph Rogueda 12 Decemb...
PDF
Choosing the right device: The case for nebulisers
PDF
Open Innovation In The Inhalation Field Academia And Industry As Partners P...
PDF
ADA APS talk 4 APS 4 April 15
PDF
Has innovation in inhalation drug delivery run out of steam?
PPTX
баскетбол
DOCX
Persyaratan umum pemukiman yang aman di daerah pesisir dan urban
PPTX
Literary criticism overview
PPTX
высоцкий
The Magic of ATMvertising
Austin Web Architecture
Radcab
Презентация
This Is How I See 2012 Results To Date
Prasanth1
Happy DevOps with Apache Cassandra and DataStax
ысоцкий
This Is How I See 2012 Results To Date231211
HFA pMDI Patents Perspectives Gold Mine Or Minefield Ph Rogueda 12 Decemb...
Choosing the right device: The case for nebulisers
Open Innovation In The Inhalation Field Academia And Industry As Partners P...
ADA APS talk 4 APS 4 April 15
Has innovation in inhalation drug delivery run out of steam?
баскетбол
Persyaratan umum pemukiman yang aman di daerah pesisir dan urban
Literary criticism overview
высоцкий
Ad

Similar to Hibernate (20)

PPTX
Hibernate tutorial
PPT
Patni Hibernate
PPTX
Hibernate ppt
PPTX
Hibernate
PPT
Hibernate
ODP
Hibernate complete Training
PPT
Hibernate introduction
PDF
Hibernate presentation
PDF
hibernate-presentation-1196607644547952-4.pdf
PPT
PDF
Hibernate interview questions
PDF
Hibernate reference1
PDF
Hibernate Interview Questions
PPT
Basic Hibernate Final
PDF
inf5750---lecture-2.-c---hibernate-intro.pdf
PPT
Hibernate for Beginners
PPTX
Hibernate in Nutshell
PPT
2010 05-21, object-relational mapping using hibernate v2
PDF
Hibernate 3
PPT
hibernate with JPA
Hibernate tutorial
Patni Hibernate
Hibernate ppt
Hibernate
Hibernate
Hibernate complete Training
Hibernate introduction
Hibernate presentation
hibernate-presentation-1196607644547952-4.pdf
Hibernate interview questions
Hibernate reference1
Hibernate Interview Questions
Basic Hibernate Final
inf5750---lecture-2.-c---hibernate-intro.pdf
Hibernate for Beginners
Hibernate in Nutshell
2010 05-21, object-relational mapping using hibernate v2
Hibernate 3
hibernate with JPA

Recently uploaded (20)

PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PPTX
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPTX
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
01-Introduction-to-Information-Management.pdf
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PDF
A systematic review of self-coping strategies used by university students to ...
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
Computing-Curriculum for Schools in Ghana
PDF
Complications of Minimal Access Surgery at WLH
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PPTX
Lesson notes of climatology university.
PDF
RMMM.pdf make it easy to upload and study
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PPTX
GDM (1) (1).pptx small presentation for students
PDF
Supply Chain Operations Speaking Notes -ICLT Program
Final Presentation General Medicine 03-08-2024.pptx
Abdominal Access Techniques with Prof. Dr. R K Mishra
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
VCE English Exam - Section C Student Revision Booklet
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
FourierSeries-QuestionsWithAnswers(Part-A).pdf
01-Introduction-to-Information-Management.pdf
Chinmaya Tiranga quiz Grand Finale.pdf
A systematic review of self-coping strategies used by university students to ...
O5-L3 Freight Transport Ops (International) V1.pdf
Computing-Curriculum for Schools in Ghana
Complications of Minimal Access Surgery at WLH
STATICS OF THE RIGID BODIES Hibbelers.pdf
2.FourierTransform-ShortQuestionswithAnswers.pdf
Lesson notes of climatology university.
RMMM.pdf make it easy to upload and study
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
GDM (1) (1).pptx small presentation for students
Supply Chain Operations Speaking Notes -ICLT Program

Hibernate

  • 2. Content covered • Introduction to Hibernate • Hibernate Architecture • O/R Mapping in Hibernate
  • 4. Configuration (org.hibernate.cfg.Configuration) • The Configuration object, as seen in the example code, is the first Hibernate object you use. It represents a configuration or properties file for Hibernate. The Configuration object is usually created once during application initialization. • The Configuration object reads and establishes the properties Hibernate uses to get connected to a database and configure itself for work. A Configuration object is used to create a SessionFactory and then typically is discarded.
  • 5. Example <?xml version="1.0" encoding="UTF-8"?> <hibernate-configuration> <session-factory> <property name="hibernate.connection.driver_class"> org.hsqldb.jdbcDriver </property> <property name="hibernate.connection.url">jdbc:hsqldb:hsql://localhost </property> <property name="hibernate.connection.username">sa</property> <property name="connection.password"></property> <property name="connection.pool_size">1</property> <property name="hibernate.dialect"> org.hibernate.dialect.HSQLDialect </property> <mapping resource="com/vaannila/student/Student.hbm.xml"/> <mapping resource="com/vaannila/student/Address.hbm.xml"/> </session-factory> </hibernate-configuration>
  • 6. SessionFactory (org.hibernate.SessionFactory) • Created from a Configuration object, and as its name implies it is a factory for Session objects. • An expensive object to create. It, like the Configuration object, is usually created during application start up. However, unlike the Configuration object, It should be created once and kept for later use.
  • 7. SessionFactory • SessionFactory object, – used by all the threads of an application. It is a thread safe object. – One SessionFactory object is created per database. – Multiple SessionFactory objects (each requiring a separate Configuration) are created when connecting to multiple databases.
  • 9. Session (org.hibernate.Session) • Provide the main interface to accomplish work with the database. Persistent objects are saved and retrieved through a Session object. • Lightweight & inexpensive to create, does the work of getting a physical connection to the database. • Not thread safe. Therefore, session objects should not be kept open for a long time. • Applications create and destroy these as needed. • When modifications are made to the database, Session objects create a Transaction object.
  • 10. Transaction (org.hibernate.Transaction) • Represents a unit of work with the database (and potentially other systems). Transactions in Hibernate are handled by an underlying transaction manager and transaction (from JDBC, JTA or CORBA). • Abstracts the developer from having to deal with the underlying transaction manager/transaction. • Transaction objects should be kept open for as short of time as possible.
  • 11. Example Sessionsession= SessionFactory.newSession(); Transaction tx = session.beginTransaction( ); tx.commit(); tx.rollback(); session.close()
  • 12. Query & Criteria • Both objects are used to retrieve (and recreate) persistent objects. • Query objects use SQL or Hibernate Query Language (HQL) string to retrieve data from the database & create objects. • Criteria enables developer to create and execute the object-oriented criteria queries.
  • 13. Hibernate O/R Mapping • ORM is a technique to map object- oriented data with the relational data. • Used to convert datatype supported in object-oriented programming language to a datatype to a data type supported by a database. (if mapping is not provided, exception is thrown while transferring an object oriented data to the database)
  • 14. Hibernate O/R Mapping • Mapping in the format that relate – Classes with tables. – Properties with columns – SQL types with Java types. • O/R Mapping can be written for any database that can be oracle, DB2, MySql, Sybase.
  • 15. Hibernate O/R Mapping • xml file XML mapping documents allows you to: – Bridge the gap between object-oriented systems and relational databases – Define the object-relational mapping – Help to generate and export database table
  • 16. Associations strategies • Mainly 3 Association strategies available in O/R mapping: • 1. One-2-one • 2. One-2-many • 3. Many-2-one • 4. Many-2-many
  • 17. Hibernate Mapping files <?xml version="1.0"?> <hibernate-mapping> <class name="com.vaannila.student.Student" table="STUDENT"> <id name="studentId" type="long" column="STUDENT_ID"> <generator class="native“/></id> <property name="studentName" type="string" not-null="true" length="100" column="STUDENT_NAME" /> <many-to-one name="studentAddress" class="com.vaannila.student.Address" column="STUDENT_ADDRESS" not-null="true" cascade="all" unique="true" /> </class> </hibernate-mapping>