Spring Data Commons

Explore common features provided by Spring Data through Spring Data Commons.

Introduction

Spring Data Commons is part of the Spring Data project that provides common technical capabilities to other Spring Data modules like Spring Data JDBC, Spring Data JPA, and Spring Data MongoDB. The primary goal of this module is to achieve technology-agnostic interfaces and abstractions commonly applicable to all the Spring Data modules specific to the underlying data sources.

The following technical capabilities are added to each Spring Data module using the shared infrastructure provided by Spring Data Commons:

  • Repository abstractions (CrudRepository and PagingAndSortingRepository)
  • Custom object mapping
  • Cross-store persistence
  • Dynamic query generation
  • Defining domain class through basic properties
  • Auditing through properties like created and lastChanged
  • Support for a custom repository

The spring-data-commons dependency

To add the Spring Data Commons support to our project, we add the spring-data-commons dependency in our build.gradle file.

Press + to interact
plugins {
id 'java'
id 'org.springframework.boot' version '3.0.2'
id 'io.spring.dependency-management' version '1.1.0'
}
group = 'com.smartdiscover'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '17'
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.data:spring-data-commons:3.0.4'
}

However, it’s often not required to add it to the project separately because the setup of Spring Data modules like Spring Data JPA, Spring Data MongoDB, and Spring Data Redis to connect a particular data source contains it already.

Object mapping of Spring Data Commons

Spring Data Commons provides basic handling of object creation and mapping to the store-native data structures available to all Spring Data modules. However, a specific Spring Data module might override the object creation/mapping to match the underlying datastore. Let’s discuss in detail how it handles the creation of the domain object and populates the properties of the object.

Create a domain object

Spring Data looks for a constructor of a domain class to create domain objects. The algorithm uses the following resolution mechanism:

  • First, it looks for a static factory method with the @PersistenceCreator annotation.

  • Alternatively, it uses the only constructor available in the domain class.

  • It uses the constructor with the @PersistenceCreator annotation if there are multiple constructors in the domain class.

  • It uses the canonical constructor if the type is a Java record.

  • It uses the no-argument constructor over other constructors to create a domain object.

Populate properties of the domain object

After creating a domain object, Spring Data tries to populate all the remaining persisting properties except those populated by the constructor used for object creation.

First, the identifier value is populated, usually in the id property. Then, persisting properties are populated with their values using the following mechanism:

  • Use the with method for the immutable property.
  • Look for the setter method of the property.
  • If mutable, set the field directly.
  • For the immutable property without the with method, look for the constructor with the @PersistenceCreator annotation.

In this lesson, we explored Spring Data Commons and its feature to handle domain object creation and mapping object properties to the underlying datastore.