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

DHRUV BANSAL picture DHRUV BANSAL · Apr 22, 2013 · Viewed 74.6k times · Source

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?

Answer

Fritz picture Fritz · Apr 22, 2013

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.