What is JPA? And what its interrelation with Hibernate?

JPA is a specification for accessing, persisting and managing the data between Java objects and the relational database, meaning it provides guidelines for developing an interface that complies with a certain standard. While JPA dictates an interface, it does not provide an implementation of that interface, meaning there is no underlying code that performs the operations to persist an object to a relational database.

Hibernate ORM (Hibernate in short) is an object-relational mapping framework for the Java language. It provides a framework for mapping an object-oriented domain model to a relational database. Hibernate solves object-relational impedance mismatch problems by replacing direct, persistent database accesses with high-level object handling functions. Hibernate’s primary feature is mapping from Java classes to database tables; and mapping from Java data types to SQL data types. Hibernate also provides data query and retrieval facilities. It generates SQL calls and relieves the developer from manual handling and object conversion of the result set.

Ok, that’s the definition for both, but what is the relationship between them?

Hibernate is the actual implementation of JPA specification. Simply imagine the JPA as an interface.

public interface JPA {
     
    public void insert(Object obj);
     
    public void update(Object obj);
     
    public void delete(Object obj);
     
    public Object select();
     
}

And Hibernate as a class that implement JPA interface.

public class Hibernate implements JPA {
 
    public void insert(Object obj) {
       //Persistence code
    }
 
    public void update(Object obj) {
       //Persistence code         
    }
 
    public void delete(Object obj) {
      //Persistence code         
    }
 
    public Object select() {
        //Persistence code
    }
     
    public Object hibernateSelect(){
        //Persistence Code
    }
}

Hibernate is a JPA provider. When you use Hibernate with JPA you are actually using the Hibernate JPA implementation. The benefit of this is that you can swap out Hibernate’s implementation of JPA for another implementation of the JPA specification. When you use straight Hibernate (hibernateSelect() from my example) you are locking into the implementation because other ORMs (Eclipse Link, OpenJPA, DataNucleus) may use different methods/configurations and annotations, therefore you cannot just switch over to another ORM.

To recap. JPA is not an implementation, it will not provide any functionality within your application. Its purpose is to provide a set of guidelines that can be followed by JPA providers to create an ORM implementation in a standardized manner. This allows the underlying JPA implementation to be swapped and for developers to easily transition (think knowledge wise) from one implementation to another.

Leave a Reply