Brief Introduction of Spring Data

Posted By :Deepak Singh Chauhan |31st October 2019

The ORM framework (Hibernate) and also the specification (JPA) area unit ideal for relative databases. However, newer databases have completely different uses.

The mission of Spring Data is to produce a family-based and consistent Spring-based programming model for Data access while not losing the special options of the underlying Data store. It facilitates the utilization of knowledge access technologies, relative and non-relational databases, map reduction frameworks and cloud-based Data services.

For the sake of simplicity, Spring Data provides associate abstraction (interface) that you simply will use in spite of the underlying Data supply.

Spring Data Commons-

  Spring Data Commons provides all the overall abstractions that permit you to attach to completely different Data stores few of them are given below.

 

1. Crud Repository-

The key interface in Spring Data Commons is CrudRepository. It provides generic CRUD operations in spite of the underlying Datastore. Extends the repository, that is that the base category for all repositories that give access to Datastores.

All strategies within the CrudRepository interface area unit as follows:

public interface CrudRepository<T, ID> extends Repository<T, ID> {
        <S extends T> S save(S entity);

        <S extends T> Iterable<S> saveAll(Iterable<S> entities);

        Optional<T> findById(ID id);

        boolean existsById(ID id);

        Iterable<T> findAll();

        Iterable<T> findAllById(Iterable<ID> ids);

        long count();

        void deleteById(ID id);

        void delete(T entity);

        void deleteAll(Iterable<? extends T> entities);

        void deleteAll();
    } 

 


2. Defining Custom Repositories-

you'll produce a custom repository extending any of the repository categories - Repository,  CrudRepository.
an associate example is shown below:

interface PersonRepository extends CrudRepository<User, Long> {

 

3. Defining custom queries-

Spring Data conjointly provides the feature of question creation from interface methodology names.
consider the instance below:

   List<Person> findByFirstNameAndLastname(String firstName, String lastname); 

The above methodology helps you search a knowledge store by passing within the given name and surname of someone. this might generate the acceptable question for the info store to come back to the personal details.

 

4. Auditing with Spring Data-

Spring knowledge conjointly provides auditing capabilities through easy annotations.

 

class Employee {

      @CreatedBy
      private User createdUser;

      @CreatedDate
      private DateTime createdDate;

      // … further properties omitted
    }

There are corresponding annotations for updates as well

  -  LastModifiedBy
   - LastModifiedDate

 

5. PagingAndSortingRepository -

Another important interface in Spring Data is PagingAndSortingRepository. PagingAndSortingRepository provides the following options to -

- Sort the data using the classification interface.
- The page is paged using the Pageable interface, which provides pagination methods: getPageNumber (), getPageSize (), next (), previousOrFirst (), etc.

 

public abstract interface PagingAndSortingRepository extends CrudRepository {

public Iterable findAll(Sort sort);

public Page findAll(Pageable pageable);

}

 

Thanks


About Author

Deepak Singh Chauhan

Deepak is a bright Java Developer, having good skills in Java, Servlet and Spring MVC Framework. His hobbies are travel and explore new places and learning about new technologies .

Request For Proposal

[contact-form-7 404 "Not Found"]

Ready to innovate ? Let's get in touch

Chat With Us