Introducing JPA and Hibernate

Posted on

In the IT world linked to Java, especially in the IT consultancy, non-professionals often associate terms such as Hibernate, JPA, like a programming language (with a lot of horrors).
The reality is different let’s try to introduce what exactly is the JPA specification, and how Hibernate is connected to it.



JPA

JPA (literally Java Persistence API) is one of the Java Enterprise Edition specifications (from 2018 it is called Jakarta EE), essentially a part of a whole series of APIs that the developer communities adhering to the Java Community Process (JCA), develop new features or simply changes and deletions of the components that are used in the JEE ecosystem. Other examples of API can be EJB, Servlet, JSP, JNDI, etc …
In our case, JPA specification has been created to manage data layer operations, but it needs to be implemented by another external component. Currently, there are several different implementations on the market and one of these is Hibernate.



HIBERNATE

Hibernate (born in 2001) is a framework for managing interactions with a database. In the early years of life, Hibernate was not related to the specifications of the Java Enterprise Edition (at the time it was written J2EE), but it was simply a separate component.

The operation is quite simple, the framework allows you to build simple Java classes. Entities that represent a data model belonging to a relational database (each Entity is connected to a Table DB) through the object-oriented approach, adding the possibility to create queries without writing instructions in SQL to perform read and write operations but using commands in Java.
Hibernate is also called Object-Relational Mapping (ORM).

Let’s see a simple example:

  @Entity 
  @Table(name = "book") 
  public class Book { 

    @Id @GeneratedValue(strategy = GenerationType.IDENTITY) 
    @Column(name = "id") 
    private Integer id; 

    @Column(name = "title") 
    private String title; 

    @Column(name="author") 
    private Integer author;
Enter fullscreen mode

Exit fullscreen mode

As you can see, without going into too much detail, you can read the annotations @Table, @Entity, and @Column) which are used by Hibernate to understand which table the class should be bound.

From version 3.2 Hibernate has officially become an implementation that follows the JPA specifications so much that JBOSS, the working group that manages the well-known “Application Server”, has taken it in its custody to bring it to be increasingly compatible with the official specification.



ADVANTAGES

1) Independence from the database, Hibernate is compatible with most products on the market (both relational and non-relational).

2) Support for HQL, we can write unique queries for each type of database using this dialect which greatly simplifies the life of the programmer.

3) Ideal for large projects where there is a need to manage many relationships between tables supporting relationships One-to-one, one-to-many, many-to-one, many-to-many.

4) Lazy loading support. Whenever we manage an entity that owns many objects, we can decide not to immediately get the child elements.



DISADVANTAGES

1) Performance. Using a tool that has many features has a cost in terms of performance, and this will certainly make your application slower. If you are dealing with projects that communicate a bunch of times with a database, perhaps Hibernate is not the right solution.

2) The high learning curve for mastering the best of the tool.



CONCLUSION

In today’s industrial world of software, ORMs are widely used to more easily manage projects of a certain size, especially where there is a need to use a tight coupling between a Java component and a DB table. There are many other ways to implement an ORM in Java, or you can simply use JDBC if your project is small in size without adding additional complexity.

Leave a Reply

Your email address will not be published.