Hibernate

Hibernate is an ORM (Object Relational Mapping) framework to map Java objects with relational (rows and columns) database tables. ORM tools map Java objects with loaded data fetched from the database for example employee details and update them back to the database with newly provided details.

It is a medium to communicate between Java apps and Databases. Apart from the hibernate framework, we also have JDBC (a Java-based tool to communicate) but Hibernate is more than that for example caching of frequently used data, lazy loading of data, etc. Just need to provide configuration details using annotation on the DBO Java class and certain configurations in the spring boot application.yml.

Requirements to perform a single Database operation:

  1. Entity / Model: If we use hibernate then we just need to annotate and mapping taken care of by Hibernate. Otherwise, if we don’t use then we need to manually map the model with the resultSet object provided by JDBC. Using JDBC, we write a lot of boilerplate code, error-prone, etc.
  2. DB connection (JDBC): JdbcTemplate is provided by Spring to connect with the database. If we use Hibernate then we don’t have to write DB connection boilerplate code instead we have to provide DB connection details username, password, port, etc in the dataSource object which is also in the configuration application.yml file.
  3. SQL Queries: Queries need to be written manually and changed every time as per the table structure if we don’t use Hibernate. If we use then we don’t need to write queries instead we need to write methods in the repo class, check the below examples.
  4. Table Schema: This one has to be done whether you are using Hibernate or other tools like JDBCTemplate. This is also a part of System Design Questions like Design Parking Lot, BookMyShow, etc

Using Hibernate, we are adding loose coupling. Suppose you want to use POSTGRESS instead of MySQL then you only need to change certain configurations in the spring boot application.yml file. Nothing else needs to change. Configuration like username, password, driver class, etc. It’s quite minimal change. No changes in the code.

We also need to know about JPA (Java Persistence API), a specification and Hibernate is an implementation of JPA. In older days, we implemented hibernate by using a separate hibernate configuration XML file. However, as time changes JPA comes into the picture as a specification or say an upper layer on Hibernate. JPA provides annotations (@Entity, @Id, @GeneratedValue, @Column, etc). Example of the JPA model.

@Entity(name="employee")
public class Employee {
   
   @Id
   @GeneratedValue
   private Integer employeeId;
   
   @Column("first_name")
   private String firstname;
   
   @Column("last_name")
   private String lastname;
    
}

Interfaces like below are used to query databases by providing methods using keywords.

@Repository
public interface EmployeeRepository extends JpaRepository<Employee, Integer> { 
         public Employee findByEmployeeId(String id); 
         
         public Employee findByFirstnameAndLastname(String firstName, String lastName);
}

Due to the above method, we don’t need to write SQL queries. SQL query internally generated by Hibernate. @Repository annotation provided by spring framework. There is a detailed explanation of writing methods as per your requirement like fetching user details based on Employee ID, FirstnameAndLastname (findByLastnameAndFirstname), etc. In the section “Query Creation” in spring-jpa documentation, you can understand the multiple syntaxes for adding multiple methods which internally create SQL queries. JPA uses And, Or, Between, Is, Equals, LessThan, LessThanEqual, etc to build queries. So, we need to use these keywords in JPA methods.

Hibernate also handles exception handling. So, hibernate also helps in removing boilerplate code as well. Also, provides transaction management. Now, we discuss about hibernate interfaces.

Hibernate interfaces:

  • SessionFactory: One instance per database connection only and the instance is created in the starting up of the application.
  • Session: It is available for per transaction. Session objects are provided by the SessionFactory object. This is utilised to perform CRUD operations with DB.

Transaction: This handles the atomic unit operation.

Leave a Reply

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