Spring Data JPA Interview Questions and Answers

What does JPA stand for?

Java Persistence API

What's the difference between JPA and Hibernate?

The JPA is a specification and Hibernate is an implement for ORM., denoting there is no implementation. We can annotate our classes as much as we would relish with JPA annotations, however, without an implementation, nothing will transpire. Think of JPA as the guidelines that must be followed or an interface, while Hibernate's JPA implementation is code that meets the API as defined by the JPA designation and provides the under the hood functionality. 
When we utilize Hibernate with JPA we are genuinely utilizing the Hibernate JPA implementation. The benefit of this is that we can swap out Hibernate's implementation of JPA for another implementation of the JPA specification. When you utilize straight Hibernate we are looking into the implementation because other ORMs(TopLink, JDO) may use different methods/configurations and annotations, consequently we cannot just switch over to another ORM.

What Is the Difference Between Hibernate and Spring Data JPA?

Hibernate is a JPA implementation, while Spring Data JPA is a JPA Data Access Abstraction. Spring Data offers a solution to GenericDao custom implementations. It can withal engender JPA queries on your behalf through method name conventions. 
Spring Data JPA is not an implementation or JPA provider, it's just an abstraction used to significantly minimize the quantity of boilerplate code required to implement data access layers for various persistence stores.

What does the @Id annotation do?

The @Id annotation is utilized to designate the identifier property of the entity bean. The placement of the @Id annotation determines the default access strategy that Hibernate will utilize for the mapping. If the @Id annotation is placed over the field, then filed access will be utilized.

What does the @Entity annotation do?

The @Entity annotation betokens a class that represents a relational table in the database. The JPA specification includes any class marked with @Entity in the persistence setup.

What does the @Column annotation do?

The @Column annotation is utilized to designate the details of the column to which a field or property will be mapped.

What does the @GeneratedValue annotation do?

The @GeneratedValue annotation is used to specify the primary key generation strategy to use. If the strategy is not specified by default AUTO will be used.

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

FetchType.LAZY = This does not load the relationships unless we invoke it via the getter method. Lazy initialization ameliorates performance by avoiding nonessential computation and minimize memory requisites.
FetchType.EAGER = This loads all the relationships. Eager initialization takes more memory consumption and processing speed is slow.

What does the @EnableJpaRepositories annotation do?

This annotation enables the automatic generation of JPA repositories. Any class which implements the CrudRepository interface will engender a repository when this annotation is present.

What does the @Query Annotation do?

In order to define SQL to execute for a Spring Data repository method, we can annotate the method with the @Query annotation — its value attribute contains the JPQL or SQL to execute.

How can we create a custom repository in Spring data JPA?

To create a custom repository, we have to extend it to any of the following interfaces:
  •  Repository
  • PagingAndSortingRepository
  •  CrudRepository
  •  JpaRepository
  • QueryByExampleRepository

What is the difference between CrudRepository, JpaRepository, and PagingAndSortingRepository interfaces in Spring Data JPA?

JpaRepository extends PagingAndSortingRepository which in turn elongates CrudRepository.
Their main functions are:
  • CrudRepository mainly provides CRUD functions.
  • PagingAndSortingRepository provides methods to do pagination and sorting records.JpaRepository provides some JPA-cognate methods such as flushing the persistence context and deleting records in a batch.
Because of the inheritance mentioned above, JpaRepository will have all the functions of CrudRepository and PagingAndSortingRepository.