Sunday 11 November 2012

Introduction To Spring Data Commons Features

This post is a quick introduction to Spring Data Common's (SDC) features.

Features

This Spring module provides a set of interfaces to manipulate data in repositories:
  • Repository - This is a marker interface indicating which object are going to access the repository.
  • CrudRepository<T, ID extends Serializable> - This interface extends Repository and provides a set of CRUD (Create, Read, Update, Delete) methods to manipulate T objects having ID as a key.
  • PageAndSortingRepository<T, ID extends Serializable> - This interface extends CrudRepository and provides method to fetch T objects in a sorted way or using pages.
  • Pageable & PageRequest - The Pageable interface is implemented by PageRequest. Page requests are created to indicate which page and page sizes should be used to fetch objects from the PageAndSortingRepository interface.
  • Page<T> - This interface contains the fetched objects returned for a given page request.
The above repository interfaces can expose too many methods for some developers. For example, one may not be comfortable with the idea of exposing write methods. The solution is to create your own customized repository interface and annotate it with:

    @RepositoryDefinition(
        domainClass=MyClass.class,
        idClass=MyClassID.class)

Then, copy the the repository methods you want to expose in this customized interface and Spring will deal with them.

There are situations where developers want to implement their own repository interfaces by extending the repository interfaces available in SDC. However, these interfaces should not be instantiated in their applications. To prevent this, they can use the @NoRepositoryBean on these customized interfaces.

More Spring related posts here.

No comments:

Post a Comment