DEV Community

Er. Bhupendra
Er. Bhupendra

Posted on

SPRING BOOT JPA INTERVIEW QUESTION 0-3 YOE

1) What is Spring Data JPA?
✅ One-Line Interview Answer (Best)

Spring Data JPA is a Spring module that simplifies database operations by providing ready-made repository methods on top of JPA, so we don’t need to write boilerplate SQL or JDBC code.

2) **Explain features of Spring Data JPA?
**

  1. Automatic repository implementation

2.Built-in CRUD operations

3.Query generation from method names

4.Pagination and sorting support

5.Support for custom JPQL and native SQL queries

6.Reduces boilerplate code

7.Easy integration with Spring projects like Spring Boot and Spring MVC

3.How to create a custom Repository class in Spring JPA?

By extending JpaRepository and adding our own methods in the interface, we can create a custom repository.
Ex:
public interface UserRepository extends JpaRepository {

User findByEmail(String email);   // custom method
Enter fullscreen mode Exit fullscreen mode

}

  1. ** Difference between CRUDRepository and JPARepository.**

CRUDRepository ==>simple crud things
JPARepository ==> extra features like sorting , pagination etc

5) Write a custom query in Spring JPA?
==> use @Query
Ex:
@Query("SELECT u FROM User u WHERE u.firstName = :firstName")
List findByFirstName(@param("firstName") String firstName);

6) What is the purpose of save() method in CrudRepository?

The save() method in CrudRepository is used to insert a new entity or update an existing entity in the database.

The @Modifying annotation is used with @Query to execute update or delete operations in Spring Data JPA.

7)** What is the use of @Modifying annotation?**
Ex:
@Modifying
@Query("UPDATE User u SET u.status = :status WHERE u.id = :id")
void updateUserStatus(@param("status") String status, @param("id") Long id);

8) Difference between findById() and getOne().
==> findById() : at same time find data with given id and
if data not found then does not given eception or error instead give empty object and data from db fast.

===> getOne : not fast , proxy lazy , exception also there

==> getOne() :
while getOne() returns a lazy proxy and fetches data only when it is accessed. If the entity does not exist, getOne() throws EntityNotFoundException.

9) Use of @temporal annotation.

10) Write a query method for sorting in Spring Data JPA.

11) Explain @Transactional annotation in Spring.

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

13) Use of @id annotation.
The @id annotation is used to specify the primary key of an entity.

14) How will you create a composite primary key in Spring JPA.

15)** What is the use of @EnableJpaRepositories method? **

16.** What are the rules to follow to declare custom methods in Repository.
**

  1. Explain QueryByExample in spring data jpa.

18) What is pagination and how to implement pagination in spring data?

19) *Explain few CrudRepository methods. *

20) Difference between delete() and deleteInBatch() methods.

21)** You need to execute a complex query that involves multiple tables and conditional logic. How do you implement this in Spring JPA?**

22) Your application requires the insertion of thousands of records into the database at once. How do you optimize this batch process using Spring JPA?

  1. ) You have entities with bidirectional relationships. How do you ensure these are correctly managed in Spring JPA to avoid common issues like infinite recursion?

24) *How do you handle schema migration in a project using Spring JPA when the schema changes due to business requirements?
*

25) You are experiencing performance issues with certain frequently accessed data. How can you implement caching in Spring JPA to improve performance?

Top comments (0)