Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Hibernate architecture


May 17, 2021 Hibernate


Table of contents


Architecture

The Hibernate schema is hierarchical, and as a data access layer, you don't have to know the underlying API. Hibernate leverages databases and configuration data to provide persistent services (and persistent objects) to applications.

Below is a very high-level view of the Hibernate application architecture.

Hibernate architecture

Below is a detailed view of hibernate application architecture and some important classes.

Hibernate architecture

Hibernate uses different existing Java APIs, such as JDBC, Java Transaction API (JTA), and Java Naming and Directory Interface (JNDI). J DBC provides a basic level of abstraction for the functionality of a common relationship database, and Hibernate supports almost any database with a JDBC driver. JNDI and JTA allow Hibernate to integrate with the J2EE application server.

The following section briefly describes each class object involved in the Hibernate application schema.

Configure the object

Configuration objects are the first Hibernate objects that you create in any Hibernate application and are often created only during application initialization. I t represents a configuration or property file required by Hibernate. Configuration objects provide two underlying components.

  • Database connection: One or more profiles that are supported by Hibernate. These files are hibernate.properties and hibernate .cfg.xml.
  • Class mapping settings: This component creates a connection between Java classes and database tables.

SessionFactory object

The configuration object is used to create a SessionFactory object, configure Hibernate for the application in turn using the provided profile, and allow instantiation of a session object. SessionFactory is a thread-safe object that is used by all threads of the application.

SessionFactory is a heavyweight object so usually it is created when the application starts and then retained for later use. E ach database requires a SessionFactory object to use a separate profile. So if you use multiple databases then you want to create multiple SessionFactory objects.

Session object

A session is used for a physical connection to the database. S ession objects are lightweight and are designed to interact with the database every time they are instantiated. Persistent objects are saved and retrieved through the Session object.

Session objects should not remain open for long periods of time because they are not normally thread-safe, and they should be created and destroyed as needed.

Transaction object

A transaction represents a unit that works with the database and most RDBMS supports transaction functionality. Transactions in Hibernate are handled by the underlying transaction manager and transactions (from JDBC or JTA).

This is a selective object that Hibernate applications may choose not to use, but to manage transactions in their own application code.

The Query object

Query objects use SQL or Hibernate query language (HQL) strings in the database to retrieve data and create objects. An instance of a query is used to link query parameters, limit the number of results returned by the query, and ultimately execute the query.

Criteria object

Criteria objects are used to create and execute objects that target rule queries to retrieve objects.