Spring Data Flashcards

1
Q

What is Spring Data Framework?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

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

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

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

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What are some key methods defined in CrudRepository interface?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are key methods defined in PagingAndSortingRepository interface?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

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

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How does Spring Data framework derive queries from method names?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

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

A

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

findFirst10ByLastname();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is Spring Data JPA?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How do you setup JPA repositories using JavaConfig based configuration?

A

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

@EnableJpaRepositories
public class MyApplicationConfig() {...}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is JPA

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What are some advantages of using JPA?

A
  • JPA reduces the burden of interacting with databases.
  • Annotation in JPA reduces the cost of creating a definition file.
  • It is user-friendly.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is the Spring data repository?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is @Query used for?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Give an example of using @Query annotation with JPQL.

A

returns matching employees from the database

@Query("select e from Employee e where se.name = ?1") 
List getEmployees(String name);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Can you name the different types of entity mapping

A

one-to-one mapping, one-to-many mapping, many-to-one mapping, and many-to-many mapping

17
Q

Define entity and name the different properties of an entity

A

?

18
Q

What is PlatformTransactionManager?

A

?

19
Q

Differentiate between findById() and getOne()

A

The findById() is available in CrudRepository while getOne() is available in JpaRepository. The findById() returns null if record does not exist while the getOne() will throw an exception called EntityNotFoundException

20
Q

Are you able to participate in a given transaction in Spring while working with JPA?

A

?

21
Q

Which PlatformTransactionManager(s) can you use with JPA?

A

?

22
Q

What do you have to configure to use JPA with Spring? How does Spring Boot make this easier?

A

?

23
Q

How are Spring Data repositories implemented by Spring at runtime?

A

?

24
Q

What does the @Id annotation do?

A

The @Id annotation marks a field as the primary key for that particular table. This is a unique identifier for each entry in the table. This annotation is typically used with @GeneratedValue to automatically generate an unique id for each entry in the table.

25
Q

What does the @Entity annotation do?

A

The @Entity annotation indicates a class represents a relational table in the database.

26
Q

What is the difference between FetchType.Eager and FetchType.Lazy?

A

FetchType attribute indicates how whether records will be eagerly or lazily loaded from the database. When records are eagerly loaded, JPA returns these objects regardless of whether they are accessed by the client or not. When records are lazily loaded the actual objects are only retrieved when directly accessed. This can save memory and processing when appropriate.

27
Q

Is the @Column annotation required for mapping fields to columns?

A

No. The @Column field allows you to optionally override the name of the column that the entity class field maps to in the database table. It is not required.