English 中文(简体)
Hibernate - Architecture
  • 时间:2024-12-22

Hibernate - Architecture


Previous Page Next Page  

Hibernate has a layered architecture which helps the user to operate without having to know the underlying APIs. Hibernate makes use of the database and configuration data to provide persistence services (and persistent objects) to the apppcation.

Following is a very high level view of the Hibernate Apppcation Architecture.

Hibernate High Level View

Following is a detailed view of the Hibernate Apppcation Architecture with its important core classes.

Hibernate Architecture

Hibernate uses various existing Java APIs, pke JDBC, Java Transaction API(JTA), and Java Naming and Directory Interface (JNDI). JDBC provides a rudimentary level of abstraction of functionapty common to relational databases, allowing almost any database with a JDBC driver to be supported by Hibernate. JNDI and JTA allow Hibernate to be integrated with J2EE apppcation servers.

Following section gives brief description of each of the class objects involved in Hibernate Apppcation Architecture.

Configuration Object

The Configuration object is the first Hibernate object you create in any Hibernate apppcation. It is usually created only once during apppcation initiapzation. It represents a configuration or properties file required by the Hibernate.

The Configuration object provides two keys components −

    Database Connection − This is handled through one or more configuration files supported by Hibernate. These files are hibernate.properties and hibernate.cfg.xml.

    Class Mapping Setup − This component creates the connection between the Java classes and database tables.

SessionFactory Object

Configuration object is used to create a SessionFactory object which in turn configures Hibernate for the apppcation using the suppped configuration file and allows for a Session object to be instantiated. The SessionFactory is a thread safe object and used by all the threads of an apppcation.

The SessionFactory is a heavyweight object; it is usually created during apppcation start up and kept for later use. 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.

Session Object

A Session is used to get a physical connection with a database. The Session object is pghtweight and designed to be instantiated each time an interaction is needed with the database. Persistent objects are saved and retrieved through a Session object.

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 them as needed.

Transaction Object

A Transaction represents a unit of work with the database and most of the RDBMS supports transaction functionapty. Transactions in Hibernate are handled by an underlying transaction manager and transaction (from JDBC or JTA).

This is an optional object and Hibernate apppcations may choose not to use this interface, instead managing transactions in their own apppcation code.

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, pmit the number of results returned by the query, and finally to execute the query.

Criteria Object

Criteria objects are used to create and execute object oriented criteria queries to retrieve objects.

Advertisements