Spring Data Flashcards
What is Spring Data Framework?
Spring Data is an umbrella project consisting of independent projects whose purpose is to unify and ease the access to different kinds of persistence stores, both relational database systems and NoSQL data stores
What are some key modules provided in the 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.
- Declare an interface extending from Repository, or one of its sub interfaces CrudRepository or PagingAndSortingRepository, passing the Entity type and ID as type parameters.
- Declare the required query methods on the interface.
- Configure Spring framework to create proxy instances for those interfaces.
- 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.
Developers just need to declare repository interfaces, and Spring Data JPA will provide the implementation automatically.
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() {...}
What is JPA
JPA stands for Java Persistence API. It is a Java specification used to persist data between the relational database and Java objects. It acts as a bridge between object-oriented domain models and relational databases. Since interaction with database from Java application is very common, JPA was created to standardize this interaction.
There are many popular JPA implementations available in the Java world like Hibernate.
What are some advantages of using JPA?
- JPA reduces the burden of interacting with databases.
- Annotation in JPA reduces the cost of creating a definition file.
- It is user-friendly.
What is the Spring data repository?
Spring data repository is a very important feature of JPA. It helps in reducing a lot of boilerplate code. Moreover, it decreases the chance of errors significantly. This is also the key abstraction that is provided using the Repository interface. It takes the domain class to manage as well as the id type of the domain class as Type Arguments.
What is @Query used for?
The @Query annotation allows you to define a Spring Data Repository method with custom SQL (both JPQL and native SQL queries). Using @Query, you can map Spring Data repository methods to actual SQL statements.
Give an example of using @Query annotation with JPQL.
returns matching employees from the database
@Query("select e from Employee e where se.name = ?1") List getEmployees(String name);