Advanced Java (2160707)

BE | Semester-4   Winter-2018 | 20-11-2018

Q1) (b)

Draw and explain hibernate architecture.

Hibernate Architecture

  • There are 4 layers in hibernate architecture
    1. Java application layer
    2. Hibernate framework layer
    3. Backend API layer
    4. Database layer.
Hibernate
(Figure: Hibernate)

Hibernate Architecture

Hibernate Architecture
(Figure: Hibernate Architecture)

Persistence

  • Persistence simply means that we would like our application’s data to outlive the applications process. In Java terms, we would like the state of (some of) our objects to live beyond the scope of the JVM so that the same state is available later.

Internal API used by Hibernate

  1. JDBC (Java Database Connectivity)
  2. JTA (Java Transaction API)
  3. JNDI (Java Naming Directory Interface)

Objects/Elements Of Hibernate Architecture

  1. Configuration Object
    • The Configuration object is the first Hibernate object you create in any Hibernate application.
    • It is usually created only once during application initialization.
    • The Configuration object provides two keys components:
      1. Database Connection:
        • This is handled through one or more configuration files supported by Hibernate. These files are hibernate.properties and hibernate.cfg.xml.
      2. Class Mapping Setup:
        • This component creates the connection between the Java classes and database tables.
  2. SessionFactory Object
    • The SessionFactory is a thread safe object and used by all the threads of an application.
    • Configuration object is used to create a SessionFactory object which in turn configures Hibernate for the application.
    • You would need one SessionFactory object per database using a separate configuration file.
    • So, if you are using multiple databases, then you would have to create multiple SessionFactory objects.
  3. Session Object
    • A Session is used to get a physical connection with a database.
    • The Session object is lightweight and designed to be instantiated each time an interaction is needed with the database.
    • The session objects should not be kept open for a long time because they are not usually thread safe and they should be created and destroyed as needed.
  4. Transaction Object
    • A Transaction represents a unit of work with the database and most of the RDBMS supports transaction functionality.
    • Transactions in Hibernate are handled by an underlying transaction manager and transaction (from JDBC or JTA).
  5. Query Object
    • Query objects use SQL or Hibernate Query Language (HQL) string to retrieve data from the database and create objects.
    • A Query instance is used to bind query parameters, limit the number of results returned by the query, and finally to execute the query.
  6. Criteria Object
    • Criteria objects are used to create and execute object oriented criteria queries to retrieve objects.