Hibernate application has four layers.
- Java application layer.
- Hibernate framework layer
- Backhand api layer
- Database layer
About JDBC , JTA , JNDI
Hibernate uses various existing Java APIs, like JDBC, Java Transaction API(JTA), and Java Naming and Directory Interface (JNDI).
JDBC provides a basic level of abstraction of functionality 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 application servers.
Hibernate Objects:The Hibernate architecture includes many objects as shown in above detailed view.
- Configuration
- SessionFactory
- Session
- TransactionFactory
- Query
- Criteria
- Configuration Object:
It is the first Hibernate object you create in any Hibernate application and usually created only once during application initialization.
It represents a configuration or properties file required by the Hibernate. The Configuration object provides two key 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:
Configuration object is used to create a SessionFactory object which inturn configures Hibernate for the application using the supplied 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 application.
The SessionFactory is heavyweight object so usually it is created during application 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.
The SessionFactory is a factory of session and client of ConnectionProvider. It holds second level cache (optional) of data. The org.hibernate.SessionFactory interface provides factory method to get the object of Session.
-
Session:
The session object provides an interface between the application and data stored in the database.
It holds a first-level cache (mandatory) of data. The org.hibernate.Session interface provides methods to insert, update and delete the object. It also provides factory methods for Transaction, Query and Criteria.
It is lightweight and designed to be instantiated each time an interaction is needed with the database. Persistent objects are saved and retrieved through a Session object.This object should not be kept open for a long time because they are not usually thread safe.
-
Transaction:
The transaction object specifies the atomic unit of work that deals with with the database and most of the RDBMS supports transaction functionality. The org.hibernate.Transaction interface provides methods for transaction management.
Transactions in Hibernate are handled by an underlying transaction manager and transaction (from JDBC or JTA).
This is an optional object and Hibernate applications may choose not to use this interface, instead managing transactions in their own application code.
-
TransactionFactory:
It is a factory of Transaction objects. It is optional.
-
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.
-
Criteria Object:
Criteria object are used to create and execute object oriented criteria queries to retrieve objects.
-
ConnectionProvider:
It is a factory of JDBC connections. It abstracts the application from DriverManager or DataSource. It is optional.