Spring Data JPA Interview Questions List in 2020

This article is targeted towards providing a comprehensive list of Spring Data JPA Interview questions, which are typical to be asked in an interview.

Many companies are adopting Spring Data JPA given it’s various features, hence you will now find it in many job descriptions.

Although it is not a regular interview topic, but if you have it on your resume, interviewer will surely be asking about it.

Hence, preparing for Spring Data JPA interview questions becomes a must.

So, without wasting any further time, let’s start with the questions and answers.

spring data jpa interview questions and answers

Spring Data JPA Interview Questions And Answers

1. What do you know about Spring data project?

Spring data project aims to help developer in developing the persistence layer – in a consistent way and easy way.

As it supports different persistence storage mechanisms – it has one project for each of these storage mechanisms. In this way, is an umbrella project under which many projects resides.

For example:

If you wants to use JDBC, it has Spring Data JDBC project to ease out the use of JDBC API in persistence layer.

Otherwise if you want to develop a JPA based persistence layer in your application , then Spring offers Spring Data JPA which works on top of JPA.

And, similarly, other projects for other such technologies.

2. Could you please name Spring data modules other than Spring data JPA and JDBC?

Of course. Spring offers:

  1. Spring data REST – to support REST communication.
  2. Spring data Redis – to support Redis store.
  3. Spring data for MongoDB – for MongoDB access in persistence layer.
  4. and similarly Spring data for Apache Cassandra, among others.

3. What is Spring data JPA project?

It is one of the Spring data projects which is used to build JPA (Java Persistence API) based data access layer within a Spring based application.

It provides different features which makes a developer’s life easier on many levels.

It does so by working on top of JPA layer and providing us some of the features out-of-the-box.

4. What is JPA (Java Persistence API)?

Java Persistence API is Java specification to manage persistence layer entities for Object-Relational mapping.

5. What is Hibernate Framework?

Hibernate is one of the JPA implementation. This means that it is an Object-Relational mapping tool to solve impedance mismatch problem.

On top of that it has multiple feature to facilitate the communication between domain layer and database.

6. What is the relationship between JPA, Hibernate and Spring data JPA?

So, JPA is just a specification and Hibernate or other similar tools like iBatis, TopLink are it’s implementations.

So, typically our application will connect with Hibernate and do it’s work. No issues.

Now, sometimes even the small communication with Hibernate can get complex and messy, un-necessarily.

Here comes the role of Spring data JPA, which sits right between application’s domain layer and JPA implementation like Hibernate. With it’s out-of-the-box features, many of the problems are solved and it gets easy to talk with any JPA implementation.

This is a bullet question. If you are able to answer this, it will assert your credibility in the interview. So, one of the most import Spring data JPA interview questions.

7. What features does Spring data JPA provides?

  1. It provides built-in out of the box support for repositories, according to our Entity classes. This alone removes almost all the typical repository boilerplate code.
  2. Query creation from method declaration. We can name methods in a specific format and it provides us the query implementation to fetch the data.
  3. It has built-in pagination support.
  4. Support for dynamic query execution.
  5. Support for Type-Safe queries by QueryDSL.

8. How to enable Spring data JPA in a project?

It can be done via. both XML and Annotation based configuration.

We use jpa namespace for XML based enabling like below:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"

  <jpa:repositories base-package="path.to.repository.package" />


For Annotation based configuration, we use @EnableJpaRepositories like this:

@EnableJpaRepositories(basePackages = "path.to.custom.repository.interfaces")
public class CustomConfigClass{

9. Could you explain some of the built-in repository classes and interfaces?

Repository Hierarchy (PagingAndSortingRepository, JpaRepository)


  • We have Repository interface at the top of the Repository interface hierarchy. All other Repository interfaces extends it. It is a market interface and on top of the hierarchy.
  • Then we have CRUDRepository which extends Repository directly. By extending this we can use CRUD operations.
  • PagingAndSortingRepository interface extends CRUDRepository. With PagingAndSortingRepository , we can use the pagination and sorting functionality for our entities.
  • And JpaRepository interface which extends PagingAndSortingRepository and QueryByExampleExecutor interfaces.
  • QueryByExampleExecutor gives us the the option of querying the data according to the logic defined in the Example object given in the interface method.
  • JpaRepositoryImplementation – It is interface to be implemented by JPARepository implemtation classes. it extends JpaRepository and

In Classes we have:

  • SimpleJpaRepository – which extends JpaRepositoryImplementation. It is the default implementation provided for CRUD operations.
  • and then we have QuerydslJpaRepository which extends SimpleJpaRepository and adds features supporting Query DSL.

This is one of the basic level Spring data JPA interview questions. It is very much expected from you to know this.

10. How to create a Spring data JPA repository?

It is a 2 step process:

  1. First we need to enable Spring data JPA in our project, as discussed above.
  2. and then, we ned to declare an interface which extends one of the Spring data JPA built-in repository and that’s it.

Spring provides it’s implementation and we do not need to do anything else.

For example:

public interface CustomRepository extends JpaRepository<CustomPojo, Serializable> {


By autowiring CustomRepository, we have whole list of methods to execute CRUD operations on CustomPojo objects.

11. What is the difference between CRUDRepository vs JPARepository? Which to extend when?

Very nice question.

CRUDRepository provides all the typical Save, Update, Delete operations for an entity.

JPARepository extends CRUDRepository as well as Repository, PagingAndSortingRepository and QueryByExampleExecutor interfaces.

So, if you just want only CRUD operations to perform: select CRUDRepository.

Else, if you want one or more features out of Pagination, Batch processing and flush operations, choose JPARepository.

Note: Spring Data JPA Interview questions starting from below which revolves around custom repository methods will be the crux of the whole interview.So, highly recommended to go through below list of questions.

12. Explain some of the CRUDRepository methods which you have used in your project.


findAll() method – It retrieves all the saved entities.

findById(ID id) method – It retrieves the entity by ID given as input.

findAllById(Iterable<ID> ids) – It retrives the list of entities which matches the list of input Ids.

existsById(ID id) – It returns true or false, depending upon the entity with input Id exists or not in the database.

save(Entity entity) – To save the entity and similarly saveAll(Iterable<ENTITY> entities) to save the list of entities.

Know that there are delete(Entity), deleteById(ID) , deleteAll(), and deleteAll(Iterable<ID> ids) methods are also there but it is rarely used by a developer in a project in comparison to other method. Hence, skip these and tell only if interviewer asks them explicitly.

13. With JPARepository, can we fetch results only by id(s)? What if we do not have the entity id?

Very valid question. If frequently happens that we do not have the entity id(s) with us to fetch the results by ids.

In this case, we can declare(note: not implement) custom methods in our repository to fetch the result according to our given input.

For example: For an Employee entity we can declare findByName(String name) or findAllByName(String name) in our JPARepository and it will fetch us the matching entities.

14. Can we declare the custom repository methods anyways? Then how does Spring data JPA understands it?

Quick answer to this question is that we can not declare the the methods in any way we want.

Actually, we need to declare the method in a fixed format. Spring data JPA understands that format and create the underlying SQL queries accordingly.

For example: if we say getByName instead of findByName, it won’t work.

15. What all rules should we follow while declaring the custom methods in a repository?

Well that would be a pretty vast discussion. But for examples:

All the fetch methods must start with findBy (case-sensitive*). After that we need to give the Entity property name by which we want to fetch the result.

For example:

If we have it in the Entity class, then findByName(String anyName) will work.

But If the Employee Entity does not have property like String name; , and we declare the method findByName, then it will throw an exception.

So, correct property name must be appended to the findBy, in a camelCase fashion.

16. How save method works in Spring data JPA?

Internally, save method calls:

persist() method – if the entity is new.

merge() method – If the entity already exists and needs to update.

17. How does Spring data JPA knows if an Entity is new or not?

It first checks the version property of the Entity. If version is null then it is new, otherwise not.

If version property doesn’t exists, it checks for identifier property.

If identifier is null, then it’s new, otherwise not.

In case we want to implement a custom condition for this decision, we can do so with the help of Persistable interface.

Make Entity class implement Persistable and then provide the implementation for isNew() method.

This question is really an internal question and again one of the most important Spring data JPA interview questions. If you are able to answer this, it will mean that you are really into the Spring data JPA and doesn’t know it just from the surface.

18. How saveAll method works in Spring data JPA?

saveAll() just calls save() in an iterative fashion.

saveAll method is annotated with @Transactional so it’s either all or none will be saved.

19. delete vs deleteInBatch in Spring data JPA?

While delete method calls EntityManager’s remove method, deleteInBatch calls executeUpdate on all the entities.

20. deleteAll vs deleteAllInBatch in Spring data JPA?

While deleteAll calls delete method iteratively, deleteAllInBatch method calls executeUpdate.

So, if there are 100 entities to be removed, deleteAll() will triggers 100 SQL queries while deleteAllInBatch() will trigger just one. This is a very important distinction, performance wise.

And by this, we are finished with Spring data interview questions on JPA.

21. What is the difference between EntityManager vs SessionFactory?

EntityManager serves the same purpose in Spring data JPA as SessionFactory serves in Hibernate.

Only difference is that SessionFactory is Hibernate specific and EntityManager is part of Java Persistence API.

22. Could you name some of the JPA annotations?

  1. @Entity – to mark a POJO class as Entity class.
  2. @Column – Property level annotation to mark the relationship between Property and database table column.
  3. @Id – to mark the corresponding property as Primary key in the database table.
  4. @GenerateValue – to automatically generate the value for this property.

Only difference is that SessionFactory is Hibernate specific and EntityManager is part of Java Persistence API.

Reference Material

Spring Data Docs

What to read next?

Checkout our curated list of Spring Boot Interview Questions if you haven’t done already.

Or go to our series on Spring Interview Questions and pick the topic suitable to your resume.

Best of Luck!!

Leave a Comment