Spring Data Framework is a Spring-based programming model for access to data.
Spring Data Framework has programming models or modules for accessing relataional databases, non-relational databases, map-reduce framewors and cloud-based data services.
Following are some key modules provided in Spring Data Framework.
Spring Data Commons - Core Spring data concepts that apply to all Spring data modules.
Spring Data JDBC - Contains Spring Data repository support for JDBC.
Spring Data JPA - Contains Spring Data repository support for JPA.
Spring MongoDBata KeyValue - Contains Spring Data repository support for key-value stores.
Spring Data LDAP - Contains Spring Data repository support for LDAP data sources.
Spring Data MongoDB - Contains Spring Data repository support for document-based MongoDB.
Spring Data Redis - Contains Spring Data repository support for Redis.
Spring Data Cassandra - Contains Spring Data repository support for Cassandra.
Spring Data Apache Solr - Contains Spring Data repository support for Apache Solr for Search based applications.
The key interfaces provided in the Spring Data Commons library are Repository, CrudRepository and PagingAndSorting.
Repository - Repository is a marker interface which takes the entity class and the ID as type arguments.
CrudRepository CrudRepositiry extends from Repository, and adds technology-agnostic CRUD operations to the entity class.
Various technology-specific abstractions such as JpaRepository, MongoRepository etc. extend from CrudRepository and add capabilities of the underlying technology on top of the generic capabilities of the CrudRepository.
PagingAndSortingRepository - PagingAndSortingRepository extends from CrudRepository and adds paging and sorting capabilities to the entity.
Some key methods defined in the Repository interface are save(S entity), findById(ID primaryKey), findAll(), long count(), void delete(T entity), boolean existsById(ID primaryKey).
Key methods defined in the PagingAndSortingRepository interface are Iterable findAll(Sort sort), Page findAll(Pageable pageable).
You can declare queries on entities and use them in four steps.
1. Declare an interface extending from Repository, or one of its sub interfaces CrudRepository or PagingAndSortingRepository, passing the Entity type and ID as type parameters.
2. Declare the required query methods on the interface.
3. Configure Spring framework to create proxy instances for those interfaces.
4. Inject repository instance in a class and execute the query methods as needed.
Spring Data framework has an in-built query builder mechanism that derives queries based on the method name.
Method names are defined as 'find...By..','count...By...' etc. 'By' acts as the delimiter to identify the start of the criteria. 'And' and 'Or' are used to define multiple criteria. 'OrderBy' is used to identify the order by criteria.
findByFirstname(String firstName);
findByFirstnameAndLastname(String firstName, String lastName);
findByFirstnameAndLastnameOrderByFirstnameAcs(String firstName, string lastName);
The number of query results can be limited by using the keywords 'first' or 'top' followed by the number of desired results.
findFirst10ByLastname();
Spring Data JPA, a part of Spring Data framework, makes it easy to implement JPA based repositories.
Spring Data JPA makes it easier to implement data access layer to JPA repositories. Developers just need to declare repository interfaces, and Spring Data JPA will provide the implementation automatically.
You setup JPA repositories using XML-based configuration using the element <jpa:repositories>.
<jpa:repositories base-package='com.interviewgrid.repositories'/>
You setup JPA repositories using JavaConfig based configuration by using the annotation @EnableJpaRepositories.
@EnableJpaRepositories
public class MyApplicationConfig() {...}