Introducing Hibernate

Hibernate, by definition, is an ORM solution for Java. Hibernate is an open source, full-fledged persistence framework. It is used to map plain old Java objects (POJOs) to the tables of a relational database and vice versa. Hibernate is used to persist application data into a data layer. Hibernate implements Java Persistence API (JPA), which is a set of standards that has been prescribed for any persistence implementation and that needs to be met in order to get certified as a Java persistent API implementation.

Hibernate sits between Java objects in memory and the relational database server to handle the persistence of objects based on O/R mapping. Hibernate supports almost all relational database engines such as the HSQL database engine, MySQL, PostgreSQL, Oracle, and so on.

The object query language used by Hibernate is called Hibernate Query Language (HQL). HQL is a SQL-like textual query language that works at the class- or field-level. Let's start learning about the architecture of Hibernate.

Hibernate architecture

In this section, we will discuss all the important elements of the Hibernate system and see how they fit into its architecture. The following figure shows the Hibernate architecture:

Hibernate architecture

Hibernate makes use of various existing Java APIs such as Java Database Connectivity (JDBC), Java Naming and Directory Interface (JNDI), and Java Transaction API (JTA). JDBC supports functionality common to relational databases, which allows almost any database with a JDBC driver to be supported by Hibernate, whereas JTA and JNDI allow Hibernate to be integrated with Java EE application servers. The basics elements of the Hibernate architecture are described in the following sections.

Configuration

The org.hibernate.cfg.Configuration class is the basic element of the Hibernate API, which allows us to build SessionFactory. Configuration can be thought of as the factory class that can produce SessionFactory. The first object of Hibernate is the configuration object, created only once during the initialization of the application. The configuration object encapsulates the Hibernate configuration details such as connection properties and dialect, which are used to build SessionFactory as shown in the following figure:

Configuration

The hibernate.properties and hibernate.cfg.xml files are configurations files that are supported by Hibernate. We can use the hibernate.properties file to specify the default values for the new configuration object.

SessionFactory

The org.hibernate.SessionFactory interface provides an abstraction for the application to obtain the Hibernate session object. The SessionFactory initialization process includes various operations that consume huge resources and extra time, so it is generally recommended to use a single SessionFactory per JVM instance. For each database, we need to have one SessionFactory using a separate configuration file. So we have to create multiple SessionFactory if we are using multiple databases.

The SessionFactory is a heavyweight and immutable towards the application; that is, it is a thread safe object. It is mostly configured as a singleton in an application so that there will be only one object per application. It is usually created during the startup of an application and is kept for later reference. The SessionFactory is used by all threads of the application. We can open multiple sessions using a single SessionFactory.

Session

The org.hibernate.Session interface is an interface between the Hibernate system and the application. It is used to get the connection with a database. It is light weight and is initiated each time an interaction is needed with the database.

Session objects are not usually thread safe and it is recommended to obtain a separate session for each thread or transaction. After we are done using session, it has to be closed to release all the resources such as cached entity objects and the JDBC connection.

The Session interface provides an abstraction for Java application to perform CRUD operations on the instance of mapped persistent classes. We will look into the methods provided by the Session interface in a later section of his chapter.

Transaction

The Transaction interface is an optional interface that represents a unit of work with the database. It is supported by most RDBMS systems. In Hibernate, Transaction is handled by the underlying transaction manager.

Query

The org.hibernate.Query interface provides an abstraction to execute the Hibernate query and to retrieve the results. The Query object represents the Hibernate query built using the HQL. We will learn about the Query interface in more detail in a later section of this chapter.

Criteria

The org.hibernate.Criteria interface is an interface to use the criterion API and is used to create and execute object-oriented criteria queries, which is an alternative to HQL or SQL.

The Persistent object

Persistent classes are the entity classes in an application. Persistent objects are objects that are managed to be in the persistent state. Persistent objects are associated with exactly one org.hibernate.Session. And once the org.hibernate.Session is closed, these objects will be detached and will be free to be used in any layer of the application.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset