Spring Data - Interview Questions

What is Spring Data Framework?

 

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.

What are some key modules provided in the Spring Data Framework?

 

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.

What are some key interfaces provided in the Spring Data Commons library?

 

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.

What are some key methods defined in CrudRepository interface?

 

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).

What are key methods defined in PagingAndSortingRepository interface?

 

Key methods defined in the PagingAndSortingRepository interface are Iterable findAll(Sort sort), Page findAll(Pageable pageable).

How do you declare and use queries using Spring Data Framework?

 

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.

How does Spring Data framework derive queries from method names?

 

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);

How do you limit the number of query results based on method name?

 

The number of query results can be limited by using the keywords 'first' or 'top' followed by the number of desired results.

findFirst10ByLastname();

What is Spring Data JPA?

 

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.

How do you setup JPA repositories using XML_based configuration?

 

You setup JPA repositories using XML-based configuration using the element <jpa:repositories>.

<jpa:repositories base-package='com.interviewgrid.repositories'/>

How do you setup JPA repositories using JavaConfig based configuration?

 

You setup JPA repositories using JavaConfig based configuration by using the annotation @EnableJpaRepositories.

@EnableJpaRepositories
public class MyApplicationConfig() {...}
 
Subscribe to our Questions

 

Spring - Interview Questions

Spring FrameworkSpring DataSpring Data JDBCSpring Data MongoDBSpring Web FlowSpring WebFluxSpring RESTful ServicesSpring Security
 
RECOMMENDED RESOURCES
Behaviorial Interview
Top resource to prepare for behaviorial and situational interview questions.

STAR Interview Example