In this post, we will see what are differences between JPA and Hibernate.
For beginners, it might be confusing what is JPA, what it does, and how it is different from Hibernate. Can we use JPA without hibernate? We are going to see these points in detail.
Java Persistent API(JPA) is a specification or set of rules. It provides specifications for persisting and managing the data. The release date of the JPA 1.0 specification was 11 May 2006. JPA is just a specification. Hibernate is the implementation of these specifications.
Hibernate is a framework that is used to communicate the java application to the database. Hibernate is an ORM tool. ORM stands for Object Relational Mapping which maps the object(entity) to data stored in the database. Hibernate was introduced in 2001 by Gavin King. See more details about Hibernate history and release here.
JPA can’t do anything alone. It must be used either with Hibernate or EclipseLink or TopLink.
The most popular combination is JPA+Hibernate.
Let’s see some basic differences between JPA and Hibernate.
JPA vs Hibernate
JPA | Hibernate |
Java Persistence API (JPA) is defined in javax.persistence package. | Hibernate is defined in org.hibernate package. |
JPA is just a specification. It contains mostly interfaces not implementation classes. For example, we have the EntityManager interface in JPA but we don’t have the corresponding implementation class i.e EntityManagerImpl in JPA. | Hibernate is a framework that is used to communicate the java application to the database. Hibernate APIs contain implementation classes. For example, in Hibernate we have Session interface, also we have SeesionImpl class that contains implementation logic for the Session interface. |
EntityManager is the core interface in JPA that contains different methods to perform CRUD operations. | Session is the core interface in Hibernate that is used to perform CRUD operations. However, Session extends EntityManger interface only. |
JPA uses Java Persistence Query Language (JPQL) to perform database operations. | Hibernate uses Hibernate Query Language (HQL) to perform database operations. |
JPA has been introduced in May 2006. | Hibernate has been introduced in May 2001. |
Can we use JPA without Hibernate?
No, We can’t use JPA alone since JPA is just a specification.
Can we use Hibernate without JPA?
Yes, we can use hibernate without JPA. When Hibernate was introduced in 2001 by Gavin King there was no existence of JPA. We can use hibernate without JPA but it is recommended to use JPA+Hiberate.
If we are using only Hibernate, we need to use classes available in org.hibernate package(i.e. org.hibernate.*
) and we should not refer to javax.persistence
anywhere in our application.
Note – We can’t use JPA without Hibernate, EclipseLink, or any other framework. Just for better understanding see below code snippet.
public interface Jpa {
void m1();
}
class Hibenate {
public void m1() {
// some implementation logic
}
}
class TopLink {
public void m1() {
// some implementation logic
}
}
class EclipseLink {
public void m1() {
// some implementation logic
}
}
Let’s see some API defined in JPA(javax.persitent package).
@Entity
@Coulmn
@CollectionTable
@Column
@ColumnResult
@ConstraintMode
@ConstructorResult
@Convert
@Converter
@Converts
@DiscriminatorColumn
@DiscriminatorValue
@ElementCollection – See more about JPA @ElementCollection here.
@Embeddable
@Embedded
@EmbeddedId
@EntityExistsException
@EntityGraph
@EntityListeners
@EntityManager
@EntityManagerFactory
Note – In JPA EntityManager is the core interface whereas in Hibernate Session is the core interface.
That’s all about JPA vs Hibernate.
You may like.
- Spring Data JPA Query Methods.
- Spring Data JPA greater than Example.
- Spring Data JPA less than Example
- Spring Data JPA IsNull Example Using Spring Boot
- Spring Data findById() Vs getOne()
- Spring Data JPA CrudRepository findById()
- Spring Data JPA JpaRepository getOne()
- Spring Data CrudRepository saveAll() and findAll().
- Spring Data CrudRepository existsById()
- Spring Data JPA delete() vs deleteInBatch()
- Spring Data JPA deleteAll() Vs deleteAllInBatch()
- Spring Data JPA JpaRepository deleteAllInBatch()
- Spring Data JPA deleteInBatch() Example
- Spring Data JPA JpaRepository saveAndFlush() Example
- Spring Data JPA CrudRepository count() Example
- Spring Data JPA CrudRepository delete() and deleteAll()
- Spring Data JPA CrudRepository deleteById() Example
- CrudRepository findAllById() Example Using Spring Boot
- Spring Data CrudRepository save() Method.
- Sorting in Spring Data JPA using Spring Boot.
- Spring Data JPA example using spring boot.
- Spring Data JPA and its benefit.
Other Spring Data JPA and Hibernate tutorials.
- @Version Annotation Example In Hibernate.
- Hibernate Validator Constraints Example Using Spring Boot.
- @Temporal Annotation Example In Hibernate/Jpa Using Spring Boot.
- Hibernate Table Per Concrete Class Spring Boot.
- Hibernate Table Per Subclass Inheritance Spring Boot.
- Hibernate Single Table Inheritance using Spring Boot.
Spring JPA docs.
Conclusion
- Java Persistent API (JPA) is an only specification which provides different API which further can be used with
- Hibernate
- TopLink
- JDO
- Eclipselink
- We can consider JPA as an interface whereas hibernate is the implementation of those interfaces.
- The initial release date of the JPA 1.0 specification is May 2006 whereas Hibernate initial release date is May 2001.