Hibernate and it’s Architecture

Hibernate is an Object Relational mapping framework. What it does is it takes rows in relational database and turns them into java objects. If we are doing Object relational mapping in our DAO with sql it breaks unexpectedly when some mistake in query (I faced this often). So hibernate enable us to get rid of sql in DAO classes. We can do our operations with db with fewer queries and less error prone.

Mappings will maps data in our java objects to the fields in relational database. We can also use annotations to do these mappings. Hibernate itself is a framework that is composed of multiple components. Hibernate interfaces with the database through JDBC which is the standard way of accessing a database in java. A configuration file is used to tell Hibernate what JDBC driver to use and what dialect to speak to the SQL database. For example the SQL used to communicate with the SQL server database is different than SQL used to communicate with a My SQL database. Hibernate also uses JNDI and JTA for communicating with an application container like JBOS and managing transactions. Hibernate basically works by reading a configuration file, which tells it what database to talk to and how to talk to it, and then reading mapping files that tell it how to map java objects to relational tables. You write java code that uses the Hibernate API to do things like Save, or Update your data, or query objects from Hibernate.

If we crack and object the Hibernate Box from above image we get the following inside

Hibernate has a few main types of classes that you’ll commonly deal with, first we have the configuration class, which is able to read a Hibernate configuration file and setup Hibernate. We need to create an instance of this class when we first start out application, so that we use Hibernate. Next we have a session factory, the session factory basically is able to manage sessions for us, we usually create a single instance of a session factory and hold onto it for the life of our application. Then e want to actually do some work with Hibernate, we ask the session factory for a session. We could execute Hibernate queries and commands directly in the session if we wanted to, but it is almost always a good idea to first ask the session to start a new transaction and then do our work and when we are done commit the transaction. So, we typically write all our java code to actually work with the persistent object using the Hibernate API in the scope of a transaction using a session to interface with Hibernate. We can also use the query and criteria classes to query our database using a SQL like syntax through the query class, or a  more object oriented syntax through the criteria class.

Balakrishna Pendyala

Author: Balakrishna Pendyala

Balakrishna is a Software Engineer working in Media Streaming Domain, and also worked on Banking and Health Care domains. His areas of interest include Object Oriented Design, SOLID Design principles, Restful Web Services and Open Source softwares including Spring, Spring Boot, and Hibernate.

If you found an error, highlight it and press Shift + Enter or click here to inform us.

Leave a Reply

Your email address will not be published. Required fields are marked *