Spring Data

Brief:

It is a data access technology based on spring framework. It can be used for relational as well as non-relational data. Spring data consists of  many sub-projects specific to databases like Spring Data Commons and Spring Data Java Persistence API (JPA). We can create repositories by extending base repositories so that we can directly use the methods to access data for example if we create JPA repository which extends CRUD repository we don't need to write queries for CRUD operations. And for complex queries we can write custom queries.


Need:

For basic CRUD operations and simple queries, developers usually spend a lot of time but using Spring Data we can save that time. Also it will help to write the structural data access code. Using this DAO implementation will be completely removed . Since only the interface methods are needed to be defined to get the needed data from database, the scope for bugs is reduced drastically.
      

How It Works:

The entities will be defined as per the JPA standards annotated entities will be used. We will take example of one entity which uses mongodb collection.
  
    @Document(collection = StateEntity.COLLECTION_NAME)
    public class StateEntity {

        public static final String COLLECTION_NAME = "state";

        public static final String COLUMN_NAME = "name";
        public static final String COLUMN_STATUS = "status";
        public static final String COLUMN_COUNTRY = "country";

        @Id
        private String id;

        @Field(COLUMN_STATUS)
        private Boolean status;

        @Field(COLUMN_NAME)
        private String name;

        @Field(COLUMN_COUNTRY)
        private CountryEntity countryEntity;

        public String getId() {
        return id;
        }

        public void setId(String id) {
        this.id = id;
        }

        public Boolean getStatus() {
        return status;
        }

        public void setStatus(Boolean status) {
        this.status = status;
        }

        public String getName() {
        return name;
        }

        public void setName(String name) {
        this.name = name;
        }

        public CountryEntity getCountryEntity() {
        return countryEntity;
        }

        public void setCountryEntity(CountryEntity countryEntity) {
        this.countryEntity = countryEntity;
        }
  
    }

  
In the above entity we added fields, collection name and id by adding annotations.

Now we will create a simple crud operation using CRUD repository. So we need to create one repository class .
  
    @Repository
    public interface StateRepository extends CrudRepository<StateEntity, Serializable> {
        public StateEntity findById(String id);
        public List<StateEntity> findByStatusIsTrue();
        public List<StateEntity> findByCountryEntityName(String name);

    }

  
This state repository extends CrudRepository and we define some methods.
  
Now we can use the methods directly by creating repository object and pass entities but for removing the dependency of a database from the presentation layer.

We create another layer as service layer in this layer we create CRUD methods and pass them models and in the implementation of a service we create entity object and pass it to repositories which are autowired in service implementation.
  
  
     public interface StateService {
            public void save(StateModel stateModel);
            public void update(StateModel stateModel);
            public StateModel getById(String id);
            public List<StateModel> getAll();
            public void delete(String id);
    }

  
Above class is a interface of StateService so that if we switch to any other database then we will just implement the service as per new database and our other implementation will remain unaffected. Now we will see the implementation.

Note : In the above class StateModel is a class with same entities as StateEntity it is used as DTO.(Data Transfer Object)
  
    @Service
    public class StateServiceImpl implements StateService {

        @Autowired
        private StateRepository stateRepository;
  
        @Override
        public void save(StateModel stateModel) {

            stateRepository.save(StateModel.createEntity(stateModel));

        }

        @Override
        public void update(StateModel stateModel) {

            stateRepository.save(StateModel.createEntity(stateModel));

        }


        @Override
        public StateModel getById(String id) {
            return StateModel.createModel(stateRepository.findById(id));
        }

        @Override
        public List<StateModel> getAll() {
            return StateModel.createListModels(stateRepository.findByStatusIsTrue());
        }


        @Override
        public void delete(String id) {

            StateEntity stateEntity = stateRepository.findById(id);
            if (stateEntity != null) {
                stateEntity.setStatus(false);
                stateRepository.save(stateEntity);
            }

        }

    }
    

Now our implementation is ready for use so following is example for how to use above services.

First we need to add autowired object of StateService.
  
    @Autowired
    private StateService stateService;
  
 For Saving data just call the method of service and pass models.
  
      stateService.save(model);
  
 Same for update and delete just call their methods
  
      stateService.update(model);
  
      stateService.delete(id);
  
 and for get all records call the getAll method
  
      stateService.getAll();
  
Another example is that if you want get record by any column value we can define method in repository directly with column name in it and it will return the record for example if u want to find by First Name then define method in repository as
  
   public StateEntity findByFirstName(String name);
    
It will parse the column name and pass the parameters and give us the record. Also we can write custom queries by implementing the defined methods in repositoryImplclass.
   

Boston Byte Grabs a Spot in Clutch’s List of Top Software Developers in Massachusetts

Boston Byte is a collective of highly-skilled and highly-professional developers dedicated to solving your technological ...