A Brief Description Of Java Object Oriented Querying

Posted By :Deepak Singh Chauhan |30th May 2019

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

Spring Data's objective is to create a family-based and consistent Spring-based programming model for data access while preserving the underlying data store's particular features. 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-

CrudRepository is the primary interface in Spring Data Commons. Provides generic CRUD operations in spite of the underlying Data store. Extends the repository, that is that the base category for all repositories that give access to Data stores.

All strategies within the CrudRepository interface area unit as follows:

<!-- HTML generated using hilite.me --><div style="background: #f8f8f8; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;"><pre style="margin: 0; line-height: 125%"><span style="border: 1px solid #FF0000"> </span>public interface CrudRepository<span style="color: #666666">&lt;</span>T, ID<span style="color: #666666">&gt;</span> extends Repository<span style="color: #666666">&lt;</span>T, ID<span style="color: #666666">&gt;</span> {
<span style="border: 1px solid #FF0000">        </span><span style="color: #666666">&lt;</span>S extends T<span style="color: #666666">&gt;</span> S save(S entity);

<span style="border: 1px solid #FF0000">        </span><span style="color: #666666">&lt;</span>S extends T<span style="color: #666666">&gt;</span> Iterable<span style="color: #666666">&lt;</span>S<span style="color: #666666">&gt;</span> saveAll(Iterable<span style="color: #666666">&lt;</span>S<span style="color: #666666">&gt;</span> entities);

<span style="border: 1px solid #FF0000">        </span>Optional<span style="color: #666666">&lt;</span>T<span style="color: #666666">&gt;</span> findById(ID <span style="color: #008000">id</span>);

<span style="border: 1px solid #FF0000">        </span>boolean existsById(ID <span style="color: #008000">id</span>);

<span style="border: 1px solid #FF0000">        </span>Iterable<span style="color: #666666">&lt;</span>T<span style="color: #666666">&gt;</span> findAll();

<span style="border: 1px solid #FF0000">        </span>Iterable<span style="color: #666666">&lt;</span>T<span style="color: #666666">&gt;</span> findAllById(Iterable<span style="color: #666666">&lt;</span>ID<span style="color: #666666">&gt;</span> ids);

<span style="border: 1px solid #FF0000">        </span><span style="color: #008000">long</span> count();

<span style="border: 1px solid #FF0000">        </span>void deleteById(ID <span style="color: #008000">id</span>);

<span style="border: 1px solid #FF0000">        </span>void delete(T entity);

<span style="border: 1px solid #FF0000">        </span>void deleteAll(Iterable<span style="color: #666666">&lt;</span><span style="border: 1px solid #FF0000">?</span> extends T<span style="color: #666666">&gt;</span> entities);

<span style="border: 1px solid #FF0000">        </span>void deleteAll();
<span style="border: 1px solid #FF0000">    </span>}<span style="border: 1px solid #FF0000"> </span>
</pre></div>
 


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 person 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