KEMBAR78
Hibernate | PPTX
Introduction to Hibernate
Sujit Kumar
Zenolocity LLC
Copyright 2012-2024 @
What is Hibernate?
• Hiberate => pure java ORM tool, ORM => Object
Relational Mapping tool. Examples of ORM:
Hibernate, Kodo, etc.
• Persist java objects to tables in a relational database.
• Java API for performing basic CRUD operations (CRUD
is create, read, update and delete)
• Java API to express queries referring to java objects
• Java API to specify metadata
• Optimization facilities : lazy loading, dirty checking
Why we need Hibernate?
• Improved productivity
– High-level object-oriented java API
– Less Java code to write
– No SQL to write

• Improved performance
– Sophisticated caching
– Lazy loading
– Eager loading

• Improved maintainability
– Less code to write

• Improved portability
– ORM framework generates database-specific SQL for you. Will
work with different databases like Oracle, MySQL, Sybase, etc.
How of Hibernate?
• Map POJOs (plain old Java objects) to
relational database tables using (XML)
configuration files called Hibernate Mapping
Files (*.hbm)
Hibernate Mapping Files
• Maps java classes to database tables.
• Maps java class attributes to the columns of a
table in an RDBMS.
• Map relationships (1 to 1, 1 to many or many
to many) between java classes and tables.
Hibernate Configuration
Two Options:
• Declarative via XML configuration in a file
called hibernate.cfg.xml
• Programmatic
hibernate.cfg.xml file
Configure the session factory with following:
• JDBC Connection configuration
• SQL variant to generate (Oracle / MySQL /…)
• Connection pool configuration
• List of mapping files which maps each java
class to a table in the database.
• Optional - create database schema from the
mapping files
Core Interfaces
•
•
•
•
•

SessionFactory interface
Session interface
Configuration interface
Transaction interface
Query and Criteria interfaces
SessionFactory interface
• The application obtains Session instances from a
SessionFactory.
• Generally, there is a single SessionFactory for the whole
application and it is created during application initialization.
• The SessionFactory caches generated SQL statements and
other mapping metadata that Hibernate uses at runtime.
• Holds cached data that has been read in one transaction
and may be reused in a future transaction.
SessionFactory sessionFactory =
configuration.buildSessionFactory();
Session Interface
• Main interface used by Hibernate applications.
• Single-threaded, short-lived object representing a
conversation between the application and the persistent
store.
• Can be used to create query objects to retrieve persistent
objects.
Session session = sessionFactory.openSession();
• Wraps a JDBC connection
• Holds a mandatory (first-level) cache of persistent
objects, used when navigating the object graph or looking
up objects by identifier
Usage
• Load the Hibernate configuration file and create
configuration object. It will automatically load all
hibernate mapping files (*.hbm)
• Create session factory from configuration object
• Get a session from this session factory
• Create HQL Query
• Execute queries which map rows in a database
table to java objects
HQL
• Object oriented extension to SQL
• Like SQL, it can create, save, update and
delete data in a persistent store.

Hibernate

  • 1.
    Introduction to Hibernate SujitKumar Zenolocity LLC Copyright 2012-2024 @
  • 2.
    What is Hibernate? •Hiberate => pure java ORM tool, ORM => Object Relational Mapping tool. Examples of ORM: Hibernate, Kodo, etc. • Persist java objects to tables in a relational database. • Java API for performing basic CRUD operations (CRUD is create, read, update and delete) • Java API to express queries referring to java objects • Java API to specify metadata • Optimization facilities : lazy loading, dirty checking
  • 3.
    Why we needHibernate? • Improved productivity – High-level object-oriented java API – Less Java code to write – No SQL to write • Improved performance – Sophisticated caching – Lazy loading – Eager loading • Improved maintainability – Less code to write • Improved portability – ORM framework generates database-specific SQL for you. Will work with different databases like Oracle, MySQL, Sybase, etc.
  • 4.
    How of Hibernate? •Map POJOs (plain old Java objects) to relational database tables using (XML) configuration files called Hibernate Mapping Files (*.hbm)
  • 5.
    Hibernate Mapping Files •Maps java classes to database tables. • Maps java class attributes to the columns of a table in an RDBMS. • Map relationships (1 to 1, 1 to many or many to many) between java classes and tables.
  • 6.
    Hibernate Configuration Two Options: •Declarative via XML configuration in a file called hibernate.cfg.xml • Programmatic
  • 7.
    hibernate.cfg.xml file Configure thesession factory with following: • JDBC Connection configuration • SQL variant to generate (Oracle / MySQL /…) • Connection pool configuration • List of mapping files which maps each java class to a table in the database. • Optional - create database schema from the mapping files
  • 8.
    Core Interfaces • • • • • SessionFactory interface Sessioninterface Configuration interface Transaction interface Query and Criteria interfaces
  • 9.
    SessionFactory interface • Theapplication obtains Session instances from a SessionFactory. • Generally, there is a single SessionFactory for the whole application and it is created during application initialization. • The SessionFactory caches generated SQL statements and other mapping metadata that Hibernate uses at runtime. • Holds cached data that has been read in one transaction and may be reused in a future transaction. SessionFactory sessionFactory = configuration.buildSessionFactory();
  • 10.
    Session Interface • Maininterface used by Hibernate applications. • Single-threaded, short-lived object representing a conversation between the application and the persistent store. • Can be used to create query objects to retrieve persistent objects. Session session = sessionFactory.openSession(); • Wraps a JDBC connection • Holds a mandatory (first-level) cache of persistent objects, used when navigating the object graph or looking up objects by identifier
  • 11.
    Usage • Load theHibernate configuration file and create configuration object. It will automatically load all hibernate mapping files (*.hbm) • Create session factory from configuration object • Get a session from this session factory • Create HQL Query • Execute queries which map rows in a database table to java objects
  • 12.
    HQL • Object orientedextension to SQL • Like SQL, it can create, save, update and delete data in a persistent store.