Spring Data JPA Interview Questions

Last modified: October 23, 2020

Ask all your coding questions @ codequery.io

Spring Data JPA

Spring Data JPA Tutorial

Spring Data JPA Example

Spring Data JPA CRUD Example

Spring Data JPA vs Hibernate

Spring Data JPA Interview Questions

1) What does JPA stand for?

Java Persistence API

2) What is the difference between Spring Data JPA and Hibernate?

Spring Data JPA provides an abstraction for more easily working with a JPA provider like Hibernate.

3) What does the @Id annotation do?

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.

4) What does the @Entity annotation do?

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

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

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.

6) Is the CrudRepository interface part of JPA?

No. CrudRepository is an interface exposed by Spring Data framework for more easily interacting with JPA implementations like Hibernate. While this interface saves a lot of boilerplate code, it isn't part of the JPA specification.

7) Is Spring Data JPA an implementation of the JPA specification?

No. Spring Data simply makes it easier to interface with a JPA specification like Hibernate. Spring Data JPA abstracts away a lot of the configuration associated with these implementations but is not an implementation itself.

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

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.

9) In a @OneToMany relationship, what does the "mappedBy" attribute indicate?

mappedBy is an attribute that specifies the owning side of a one to many relationship. For example, if you want to establish a one to many relationship between authors and books you would specify mappedBy="authors" on the Books entity.

10) What does the @EnableJpaRepositories annotation do?

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

11) What is Object Relational Mapping (ORM)

ORM is a mechanism for maintaining the relationship between object oriented data structures and relational tables in a database. Hibernate is an ORM tool that implements the JPA specifications. ORM allows objects (POJOS) to represent database tables and serves as an abstraction for database querying.

12) What are some of the most popular ORM frameworks?

Hibernate, TopLink, ORMLite, iBATIS, JPOX

13) What does the @Query Annotation do?

The @Query annotation allows you to define a Spring Data Repository method with custom SQL. Using @Query, you can map Spring Data repository methods to actual SQL statements.

14) What's the difference between a CrudRepository and JpaRepository in Spring Data JPA?

CrudRepository extends REpository interface. JpaRepostitory extends PagingandSortingRepository interface. CrudRepository is for CRUD methods only where batch operations are better handled by extending JpaRepository.

15) How does the CrudRepository save() method work in Spring Data JPA

The save() method effectively "upserts" a record. If the record doesn't exist in the database, then persist() is called. If the record does exist, then merge() is called to perform an update.

You might also like: