Introduction to Spring Data

1. Introduction

No better way to brief about Spring Data. Hence quoting the lines from the official Spring Data documentation

Spring Data’s mission is to provide a familiar and consistent, Spring-based programming model for data access while still retaining the special traits of the underlying data store.

It makes it easy to use data access technologies, relational and non-relational databases, map-reduce frameworks, and cloud-based data services. This is an umbrella project which contains many subprojects that are specific to a given database. The projects are developed by working together with many of the companies and developers that are behind these exciting technologies.

Spring Data comes up with a generic model for all types of data stores. While Spring Data JPA deals with relational databases, Spring Data also provides a generic framework to NoSQL databases like MongoDB using their individual modules. Like MongoDB specific functionalities can be provided using Spring Data MongoDB.

1.1 Main Modules

  • Spring Data Commons
  • Spring Data JPA
  • Spring Data KeyValue
  • Spring Data LDAP
  • Spring Data MongoDB
  • Spring Data Gemfire
  • Spring Data REST
  • Spring Data Redis
  • Spring Data for Apache Cassandra
  • Spring Data for Apache Solr

In this tutorial, we will be particularly focusing on Spring Data JPA and Spring Data REST.

2. Spring Data JPA

Spring Data JPA is an added layer of abstraction on top of JPA, defining a “standarized” design for the support of your Persistence Layer in a Spring context. It provides with a unique GenericDao custom implementation that could generate corresponding JPA queries on our behalf just by method name conventions.

2.1 JPA vs Spring Data JPA

Out of the box, Spring Data JPA provides DAO that has advanced CRUD features and eases up the job of developers by extensively eliminating the earlier JPA boilerplate codes, where you don’t actually need to prepare any JDBC or write Criteria Queries or deal with your entity manager. With Spring Data JPA, there are no more such hassles. Spring Data JPA just needs you to write an DAO interface (we call it a Repository in Spring Data terms), and in turn it very generously generates an SQL for you.

2.2 JPA vs Spring Data JPA vs Hibernate

Hibernate is a JPA implementation, while Spring Data JPA is an added layer of abstraction on top of JPA. Spring Data provides with a unique GenericDao custom implementation that could generate corresponding JPA queries on our behalf just by method name conventions.

With Spring Data JPA, we may use Hibernate, Eclipse Link or any other JPA provider. So does that mean Spring Data JPA cannot exist by itself? That is, under the hood, it uses one of the JPA provider (like Hibernate, Eclipselink or any other JPA provider)? That’s right. Spring JPA still requires a JPA provider.

Spring JDBC is much more light-weight, super-fast and is intended for native querying, and if you only intend to use JDBC alone, then you are better off using Spring JDBC and overcome JDBC verbosity.

So Hibernate and Spring Data are complementary rather than competitors.

2.3 Spring Data REST

Spring Data JPA is the spring way to access data using JPA. We could use Spring Data REST on top of Spring Data JPA to create a REST-API layer with no code on top of your repositories and entities.

Spring Data REST is the fastest way to create a REST API on top of your JPA layer. Though its’ highly customizable, but the most significant weakness still is the tight coupling between entities and API. However, Spring Data REST is fast and can get us going by just writing the minimal amount of code.

A Spring alternative to Spring Data REST is using Spring MVC directly to create a REST API on our own. Spring Data JPA could still be used to implement the data access layer. Spring MVC is very powerful and is used by Spring Data REST under the hood, giving us full control of the REST layer.

Spring Data Abstraction

Image Credits – Zeroturnaround

Receive our updates to your inbox

Get more stuff like this
in your inbox

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.