Skip to content

prasanna0586/spring-data-dynamodb

 
 

Repository files navigation

Build Status Maven Central codecov Coverage Report Last Commit

Spring Data DynamoDB

Spring Data DynamoDB makes it easy to build Spring-powered applications that use AWS DynamoDB for persistence, following the familiar patterns of the Spring Data family.

This module is built on AWS SDK v2.

Supported Features

  • Implementation of CRUD methods for DynamoDB entities
  • Dynamic query generation from query method names
  • @Query annotation with filter expressions
  • Transparent handling of Hash and Hash/Range key entities
  • Global Secondary Index (GSI) and Local Secondary Index (LSI) support
  • Pagination and sorting support
  • Projections and custom repository implementations
  • Built-in batch operations with automatic retry and exponential backoff
  • Event listeners (BeforeSave, AfterSave, BeforeDelete, AfterDelete, etc.)
  • Auditing support via @EnableDynamoDBAuditing
  • GraalVM native image compatibility
  • REST support via spring-data-rest

Requirements

  • Java 21+
  • Spring Boot 3.x or Spring Framework 6.x
  • AWS SDK v2

Quick Start

Download the JAR through Maven Central

<dependency>
  <groupId>io.github.prasanna0586</groupId>
  <artifactId>spring-data-dynamodb</artifactId>
  <version>7.0.0</version>
</dependency>

You also need AWS SDK v2 DynamoDB Enhanced Client:

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>software.amazon.awssdk</groupId>
            <artifactId>bom</artifactId>
            <version>2.38.1</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<dependencies>
    <dependency>
        <groupId>software.amazon.awssdk</groupId>
        <artifactId>dynamodb-enhanced</artifactId>
    </dependency>
</dependencies>

Create a DynamoDB entity User:

@DynamoDbBean
public class User {

    private String id;
    private String firstName;
    private String lastName;

    @DynamoDbPartitionKey
    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
}

Create a CRUD repository interface UserRepository:

@EnableScan
public interface UserRepository extends CrudRepository<User, String> {

    List<User> findByLastName(String lastName);

    Optional<User> findByFirstName(String firstName);
}

or for paging and sorting...

public interface PagingUserRepository extends PagingAndSortingRepository<User, String> {

    @EnableScan
    Page<User> findByLastName(String lastName, Pageable pageable);

    @EnableScan
    @EnableScanCount
    Page<User> findAll(Pageable pageable);
}

Create the configuration class DynamoDBConfig:

@Configuration
@EnableDynamoDBRepositories(basePackages = "com.example.repository")
public class DynamoDBConfig {

    @Bean
    public DynamoDbClient amazonDynamoDB() {
        return DynamoDbClient.builder()
            .region(Region.US_EAST_1)
            .build();
    }
}

Note: You only need to define a DynamoDbClient bean. The library creates DynamoDbEnhancedClient internally.

Documentation

For complete documentation, examples, and advanced topics, visit the User Guide.

Topics covered include:

Migration from SDK v1

If you are migrating from version 6.x (AWS SDK v1) to version 7.x (AWS SDK v2), please refer to the Migration Guide.

Key changes include:

  • New AWS SDK v2 annotations (@DynamoDbBean, @DynamoDbPartitionKey, etc.)
  • Simplified configuration - only DynamoDbClient bean required
  • Marshalling modes for backward compatibility with existing data

Example Project

A complete working example is available at validate-spring-data-dynamodb.

This example demonstrates GSI annotations, optimistic locking, custom converters, @Query annotations with filter expressions, and integration tests with Testcontainers.

Version & Spring Framework compatibility

The major and minor number of this library refers to the compatible Spring framework version. The build number is used as specified by SEMVER.

spring-data-dynamodb version Spring Boot compatibility Spring Framework compatibility Spring Data compatibility AWS SDK
6.0.3 >= 3.2.5 >= 6.1.6 2023.1.5 v1
6.0.4 >= 3.5.6 >= 6.2.11 2025.0.4 v1
7.0.0 >= 3.5.6 >= 6.2.11 2025.0.4 v2

spring-data-dynamodb depends directly on spring-data as well as spring-tx.

compile and runtime dependencies are kept to a minimum to allow easy integration, for example into Spring Boot projects.

Development

Running Tests

Tests use Testcontainers which automatically manages DynamoDB Local. Just ensure Docker is running:

mvn clean verify

Additional Documentation

About

This module deals with enhanced support for a data access layer built on AWS DynamoDB.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Java 100.0%