What's the difference between JPA and Spring Data JPA?

2013-04-22 java hibernate jpa orm spring-data-jpa

I am a bit confused about the difference between Spring Data-JPA and JPA. I know about JPA that it is a specification for persisting the Java Objects to a relational database using popular ORM technology.

In other words, JPA provides interfaces and other ORM technologies, implements those interfaces known as JPA provider e.g Hibernate.

Now, what exactly is Spring Data JPA?

Is Spring Data JPA has added some more functionality (Interfaces) over JPA and still it is specified only or it is also a JPA provider?

I saw Spring Data JPA works around repositories (DAO layer: if I am not wrong). So, I mean, how it is different using 'Spring Data JPA + Hibernate' or only using 'Hibernate' directing?

Answers

The Java Persistence API, sometimes referred to as JPA, is a Java framework managing relational data in applications using the Java Platform, Standard Edition (JavaSE) and Java Platform, Enterprise Edition(JavaEE).

Persistence in this context covers three areas:

  • The API itself, defined in the javax.persistence package.

  • The Java Persistence Query Language (JPQL).

  • Object-Relational metadata.

    enter image description here

Spring Data JPA is part of the umbrella Spring Data project that makes it easier to implement JPA based repositories.

Features:

  • Sophisticated support to build repositories based on Spring and JPA
  • Support for QueryDSL predicates and thus type-safe JPA queries
  • Transparent auditing of domain class
  • Pagination support, dynamic query execution, ability to integrate custom data access code
  • Validation of @Query annotated queries at bootstrap time
  • Support for XML based entity mapping
  • JavaConfig based repository configuration by introducing @EnableJpaRepositories

    enter image description here

I saw Spring, JPA works around repositories (DAO layer: if I am not wrong). So I mean how it is different using 'Spring JPA + Hibernate' or only using 'Hibernate' directly?

As you said, JPA is an specification while Hibernate is a particular implementation of that specification (these implementations are usually referred to as Providers). By using Hibernate you tie yourself to that provider restricting your freedom to switch to another option when required (for example, you want to use EclipseLink or ObjectDB instead because Hibernate has a bug that halts your development process).

Quoting Spring Data JPA's documentation:

Implementing a data access layer of an application has been cumbersome for quite a while. Too much boilerplate code had to be written. Domain classes were anemic and haven't been designed in a real object oriented or domain driven manner.

Using both of these technologies makes developers life a lot easier regarding rich domain model's persistence. Nevertheless the amount of boilerplate code to implement repositories, especially is still quite high. So the goal of the repository abstraction of Spring Data is to reduce the effort to implement data access layers for various persistence stores significantly.

To sum it up, it is on top of JPA adding another layer of abstraction, kind of defining a standard-based design to support Persistence Layer in a Spring context. Those defined interfaces (known to Spring) provide the services that the framework handles using JPA to serve the results. You define a repository in a way Spring can scan the project and find it:

<repositories base-package="com.acme.repositories" />

Thus, allowing you to use it in the context of a container or outside of it.

Now what exactly is Spring, JPA. Is Spring, JPA has added some more functionality (Interfaces) over JPA and still it is specified only or it is also a JPA provider?

Spring Data JPA provides a definition to implement repositories that are supported under the hood by referencing the JPA specification, using the provider you define.

JPA

JPA is a specification that standardizes the way Java Objects are mapped to a relational database system. Being just a specification, JPA consists of a set of interfaces, like EntityManagerFactory, EntityManager, and annotations that help you map a Java entity object to a database table.

There are several JPA providers, like HIbernate, EclipseLink, or Open JPA which you can use.

Spring Data JPA

Spring Data JPA is a JPA data access abstraction. Just like JPA, Spring Data JPA cannot work without a JPA provider.

Spring Data JPA offers a solution to the DDD Repository pattern or the DAO (Data Acess Object) pattern. It can also generate JPA queries on your behalf through method name conventions.

Spring Data JPA can work with Hibernate, Eclipse Link, or any other JPA provider. A very interesting benefit of using Spring or Java EE is that you can control transaction boundaries declaratively using the @Transactional annotation.

Related