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.
Map
-based repositories and SPIs to easily build a Spring Data module for key-value stores.Spring Data is an umbrella project consisting of independent projects with, in principle, different release cadences. To manage the portfolio, a BOM (Bill of Materials - see this example) is published with a curated set of dependencies on the individual project. The release trains have names, not versions, to avoid confusion with the sub-projects.
The names are an alphabetic sequence (so you can sort them chronologically) with names of famous computer scientists and software developers. When point releases of the individual projects accumulate to a critical mass, or if there is a critical bug in one of them that needs to be available to everyone, the release train will push out “service releases” with names ending “-SRX”, where “X” is a number.
Currently the release train contains the following modules:
For a quick taste, look at the following domain object:
@Entity
public class Employee {
private @Id @GeneratedValue Long id;
private String firstName, lastName, description;
private Employee() {}
public Employee(String firstName, String lastName, String description) {
this.firstName = firstName;
this.lastName = lastName;
this.description = description;
}
}
This defines a simple JPA entity with a few fields. The following code shows a simple repository definition:
public interface EmployeeRepository extends CrudRepository<Employee, Long> {
Employee findByFirstName(String firstName);
List<Employee> findByLastName(String lastName);
}
This interface extends Spring Data’s CrudRepository
and defines the type (Employee
) and the id type (Long
). Put this code inside a Spring Boot application with spring-boot-starter-data-jpa
like this:
@SpringBootApplication
public class MyApp {
public static void main(String[] args) {
SpringApplication.run(MyApp.class, args);
}
}
Launch your app and Spring Data (having been autoconfigured by Boot, SQL or NoSQL) will automatically craft a concrete set of operations:
save(Employee)
delete(Employee)
find(Employee)
find(Long)
findAll()
On top of the CRUD operations inherited from CrudRepository
, the interface defines two query methods.
findByFirstName(…)
automatically writes a JPA query based on firstName and only return the first employee found.findByLastName(…)
automatically writes a JPA query based on lastName and returns a collection.If you are using Spring Boot, you will inherit predefined versions for each project. To plugin a newer or older release train, configure the spring-data-releasetrain.version
property to the release train iteration name you want to use.
Spring Boot provides so called started POMs for a variety of SPring Data modules which pull in a curated set of dependencies you’ll need to use the individual modules. For details on that see the Spring Boot reference documentation.