Table of Contents

One of the most significant parts of a real-world application is the place where the data is stored. It affects the internal structure and performance of the whole application, and it may also simplify or complicate its future development. Considering that there are lots of databases (such as MySQL, Postgres, MongoDB, Redis, and many others), the way we interact with them becomes exceptionally important.

Providing a huge and diverse foundation for developing production-ready applications, the Spring framework couldn’t ignore this crucial issue. As a consequence, one of the most important Spring parts (or subframeworks), which is known as Spring Data, focuses entirely on data access and object mapping. It is designed to support a simple and unified way for data access. This approach is almost independent of the database type but still allows developers to take advantage of special features of the chosen database.

When using Spring Data, you don’t usually need to consider low-level database details. Instead, you can concentrate on your object model, while Spring Data automatically converts your class objects to the required format of the database.

Features

Spring Data provides a huge number of features to facilitate interactions with databases. Here is the list of the most essential of them.

  • Repository-oriented programming. Spring Data includes a concept named Repository that hides all the database-specific details and provides a high-level interface to interact with the database in an object-oriented manner. This approach allows developers to design a consistent data access level regardless of the database type.
  • Query generation based on repository method names. Database queries are generated automatically (e.g. SQL queries) based on the names of repository methods.
  • Database configuration. Spring Data provides a way to configure the interaction with a database using the configuration file or setting it up programmatically.
  • Repository customization and native queries. When the query generation is not enough, it is possible to customize your repositories to execute some database-specific queries.
  • Integration with other parts of Spring. The Spring Data Framework is seamlessly integrated into the Spring Ecosystem. The repositories can be used together with other Spring components. At the same time, it doesn’t rely on Spring Web, so it can be used in desktop and other types of Spring applications.
  • Audit support. Spring data provides the possibility to keep track of who and when created or changed an object stored in a database.

Of course, it is not the full list of features provided by Spring Data, but these are the most important for beginners.

Modules

Spring Data consists of a set of modules to support working with different types of databases. The number of Spring Data modules is constantly growing and the full list looks really impressive. Some of the modules are being developed under the Pivotal guidance, while others are fully community-driven.

Here are some of those modules with brief descriptions.

  • Spring Data Commons is a core module that provides the most common functionality for interacting with all databases, including repositories, transactions, audits, and so on.
  • Spring Data JDBC provides a way to access relational databases using Java DataBase Connectivity API (JDBC) together with the repository-oriented approach.
  • Spring Data JPA is aimed at working with any relational databases by means of the commonly used Java Persistence API (JPA) tool, which is a more advanced alternative to JDBC.
  • Spring Data MongoDB is designed to work only with MongoDB using Spring Data repositories.
  • Spring Data Redis is designed to access Redis from Spring applications.
  • Spring Data REST allows exporting Spring Data repositories as REST services. Unlike others, this module relies on Spring Web and it is intentionally designed to be used in web applications.

There are also modules for storing your data in other databases, for example, Apache Cassandra and Neo4J, as well as to interact with more specialized storage systems such as LDAP or Hazelcast. You don’t have to include all these modules in your application. Depending on the database or storage you use, you will add one or more of them.

You don’t need to add Spring Data Commons manually to your application because other database-specific modules already include it.

Conclusion

Spring Data is a part of Spring that comes in handy when you need to access a database. It provides a unified way to interact with different databases using repositories, which hide low-level database-specific queries internally. One of the distinctive features of the Spring Data framework is its high modularity. There is one core module and multiple independent database-specific modules for all occasions (relational databases, MongoDB, Redis, and so on). In addition, most Spring Data modules do not rely on Spring Web and, as a consequence, you are not limited to using them in web applications.

Leave a Reply

Your email address will not be published.