1. Overview

In this quick guide, we're going to focus on how to integrate the Spring Data API with the Apache Ignite platform.

To learn about Apache Ignite check out our previous guide.

2. Maven Setup

In addition to the existing dependencies, we have to enable Spring Data support:

<dependency>
    <groupId>org.apache.ignite</groupId>
    <artifactId>ignite-spring-data</artifactId>
    <version>${ignite.version}</version>
</dependency>

The ignite-spring-data artifact can be downloaded from Maven Central.

3. Model and Repository

To demonstrate the integration, we'll build an application which stores employees into Ignite's cache by using a Spring Data API.

The POJO of the EmployeeDTO will look like this:

public class EmployeeDTO implements Serializable {
 
    @QuerySqlField(index = true)
    private Integer id;
    
    @QuerySqlField(index = true)
    private String name;
    
    @QuerySqlField(index = true)
    private boolean isEmployed;

    // getters, setters
}

Here, the @QuerySqlField annotation enables querying the fields using SQL.

Next, we'll create the repository to persist the Employee objects:

@RepositoryConfig(cacheName = "baeldungCache")
public interface EmployeeRepository 
  extends IgniteRepository<EmployeeDTO, Integer> {
    EmployeeDTO getEmployeeDTOById(Integer id);
}

Apache Ignite uses its own IgniteRepository which extends from Spring Data's CrudRepository. It also enables access to the SQL grid from Spring Data.

This supports standard CRUD methods, except a few that don't require an id. We'll take a look at why in more detail in our testing section.

The @RepositoryConfig annotation maps the EmployeeRepository to Ignite's baeldungCache.

4. Spring Configuration

Let's now create our Spring configuration class.

We'll use the @EnableIgniteRepositories annotation to add support for Ignite repositories:

@Configuration
@EnableIgniteRepositories
public class SpringDataConfig {

    @Bean
    public Ignite igniteInstance() {
        IgniteConfiguration config = new IgniteConfiguration();

        CacheConfiguration cache = new CacheConfiguration("baeldungCache");
        cache.setIndexedTypes(Integer.class, EmployeeDTO.class);

        config.setCacheConfiguration(cache);
        return Ignition.start(config);
    }
}

Here, the igniteInstance() method creates and passes the Ignite instance to IgniteRepositoryFactoryBean in order to get access to the Apache Ignite cluster.

We've also defined and set the baeldungCache configuration. The setIndexedTypes() method sets the SQL schema for the cache.

5. Testing the Repository

To test the application, let's register the SpringDataConfiguration in the application context and get the EmployeeRepository from it:

AnnotationConfigApplicationContext context
 = new AnnotationConfigApplicationContext();
context.register(SpringDataConfig.class);
context.refresh();

EmployeeRepository repository = context.getBean(EmployeeRepository.class);

Then, we want to create the EmployeeDTO instance and save it in the cache:

EmployeeDTO employeeDTO = new EmployeeDTO();
employeeDTO.setId(1);
employeeDTO.setName("John");
employeeDTO.setEmployed(true);

repository.save(employeeDTO.getId(), employeeDTO);

Here we used the save*(key, value)* method of IgniteRepository. The reason for this is that the standard CrudRepository save(entity), save(entities), delete(entity) operations aren't supported yet.**

The issue behind this is that the IDs generated by the CrudRepository.save() method are not unique in the cluster.

Instead, we have to use the save*(key, value), save(Map<ID, Entity> values), deleteAll(Iterable ids)* methods.

After, we can get the employee object from the cache by using Spring Data's getEmployeeDTOById() method:

EmployeeDTO employee = repository.getEmployeeDTOById(employeeDTO.getId());
System.out.println(employee);

The output shows that we successfully fetched the initial object:

EmployeeDTO{id=1, name='John', isEmployed=true}

Alternatively, we can retrieve the same object using the IgniteCache API:

IgniteCache<Integer, EmployeeDTO> cache = ignite.cache("baeldungCache");
EmployeeDTO employeeDTO = cache.get(employeeId);

Or by using the standard SQL:

SqlFieldsQuery sql = new SqlFieldsQuery(
  "select * from EmployeeDTO where isEmployed = 'true'");

6. Summary

This short tutorial shows how to integrate the Spring Data Framework with the Apache Ignite project. With the help of the practical example, we learned to work with the Apache Ignite cache by using the Spring Data API.

As usual, the complete code for this article is available in the GitHub project.