Top JPA Interview Questions (2023) | CodeUsingJava








Most frequently asked JPA Interview Questions


  1. What is JPA?
  2. What is the difference between JPA and Hibernate?
  3. What are the advantages of JPA?
  4. What is Object-Relational Mapping?
  5. In JPA EntityManager why use persist() over merge()?
  6. What are the different types of entity mapping ?
  7. What are the properties of an entity?
  8. What is the role of Entity Manager in JPA?
  9. What is the Spring data repository?
  10. What is the difference between JPA and JDO?
  11. How can we create a custom repository in Spring data JPA?
  12. Defining a JPA Entity Class?
  13. What is an Entitymanager?
  14. What is difference between CrudRepository and JpaRepository interfaces in Spring Data JPA?
  15. What data types are allowed in the Attributes of the Entity Class?
  16. What is the Embeddable class?
  17. What requirements does JPA set for Embeddable classes?

What is JPA?

  • The Java Persistence API (JPA) is a Java application programming interface specification that describes the management of relational data in applications using Java Platform, Standard Edition and Java Platform, Enterprise Edition.
  • Java Persistence API is a collection of classes and methods to store the large quantity of data into a database.


JPA and Hibernate

What is the difference between JPA and Hibernate ?

  • Hibernate is one of the most popular open source implementations of the latest specification (JPA 2.1).
  • JPA only describes rules and APIs and Hibernate implements these descriptions, but Hibernate has additional features that are not described in JPA.
  • JPA defines the specification. It is an API.
  • Hibernate provides additional features on top of JPA. But depending on them would mean a lock in to Hibernate.


What are the advantages of JPA ?

The advantages of JPA are as follows:
  • The application development process is enhanced by ObjectDB which is built to support the JPA
  • It increases the developers productivity as the code for mapping the database tables and the domain models are written once and can be used for other operations
  • It is database independent. JPA provides an independent abstraction layer on top of SQL.


advantages of JPA

What is Object-Relational Mapping ?

  • Object-relational mapping (ORM, O/RM, and O/R mapping) in computer software is a programming technique for converting data between incompatible type systems in relational databases and object-oriented programming languages.
  • This creates, in effect, a "virtual object database" that can be used from within the programming language.
  • We wrap the tables or stored procedures in classes in the programming language, so that instead of writing SQL statements to interact with our database, we use methods and properties of objects.


In JPA EntityManager why use persist() over merge() ?

  • Persist: Persist takes an entity instance and adds it to the context making that instance managed.
  • Insert a new register to the database.
  • Merge:
  • Merge creates a new instance of your entity, copies the state from the supplied entity and then makes the new copy managed.
  • Find an attached object with the same id and update it.

What are the different types of entity mapping ?

Following are the types of object-relational mapping: -
  • One-to-one mapping: This mapping represents a single-valued association where an instance of one entity is associated with an instance of another entity. This means that one instance of source entity can be mapped with at most one instance of the target entity.
  • One-To-Many mapping: This mapping comes into the category of collection-valued association where an entity is associated with a collection of other entities. This means that the instance of one entity can be mapped with any number of instances of another entity.
  • Many-to-one mapping: This mapping represents a single-valued association where a collection of entities can be associated with the similar entity. This means that more than one row of an entity can refer to the same row of another entity.
  • Many-to-many mapping: This mapping represents a collection-valued association where any number of entities can be associated with a collection of other entities. This means that more than one row of one entity can refer to more than one row of another entity.


What are the properties of an entity ?

  • Persistability: An object is called persistent if it is stored in the database and can be accessed anytime.
  • Persistent Identity: In Java, each entity is unique and represents an object identity. Similarly, when the object identity is stored in a database, then it is represented as persistence identity. This object identity is equivalent to the primary key in the database.
  • Transactionality: A transaction is a set of operations that either fail or succeed as a unit. Transactions are a fundamental part of persistence.
  • Granularity: Entities should not be primitives, primitive wrappers or built-in objects with single dimensional state.


What is the role of Entity Manager in JPA?

An entity manager is responsible for the following roles in JPA.
  • The entity manager has the role to manage an object referenced by an entity.
  • It implements the API and encapsulates all of them within a single interface.
  • It is used for operations like read, delete and write an entity.


What is the Spring data repository?

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.
These Repositories are Java interfaces that allow you as the developer to define a data access contract. The Spring Data JPA framework can then inspect that contract, and automatically build the interface implementation under the covers for you.


What is the difference between JPA and JDO?

JPA and Java Data Objects (JDO) are two specifications for storing java objects in databases. If JPA is concentrated only on relational databases, then JDO is a more general specification that describes the ORM for any possible bases and repositories.
JDO, from an API point of view, is agnostic to the technology of the underlying datastore, whereas JPA is targeted to RDBMS datastores.
JPA can be viewed as part of the JDO specification specialized in relational databases, even though the API of these two specifications does not completely match. The developers of specifications also differ if JPA is developed as JSR, then JDO was first developed as JSR, now it is developed as an Apache JDO project.


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

The four major components of Kafka are:
  • Repository
  • PagingAndSortingRepository
  • CrudRepository
  • JpaRepository
  • QueryByExampleRepository

Defining a JPA Entity Class?

A JPA entity class is a Plain Old Java Object class.Eg. an ordinary Java class that is marked as having the ability to represent objects in the database. Conceptually this is similar to serializable classes, which are marked as having the ability to be serialized.

JPA Entity Class


What is an Entitymanager?

The EntityManager is an API that manages the lifecycle of entity instances. They manages a set of entities that are defined by a persistence unit. Each EntityManager instance is associated with a persistence context.
The EntityManager API is used to create and remove persistent entity instances, to find entities by their primary key, and to query over entities. The set of entities that can be managed by a given EntityManager instance is defined by a persistence unit.


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

CrudRepository provides CRUD functions. PagingAndSortingRepository provides methods to do pagination and sort records.
JpaRepository provides JPA related methods such as flushing the persistence context and delete records in a batch.


What data types are allowed in the Attributes of the Entity Class?

The fields or properties must be of the following Java language types:
  • Java primitive types
  • java.lang.String
  • java.math.BigInteger
  • java.math.BigDecimal
  • java.util.Date
  • java.util.Calendar
  • java.sql.Date
  • java.sql.Time
  • java.sql.TimeStamp

What is the Embeddable class?

Embeddable classes are used to represent the state of an entity but don't have a persistent identity of their own, unlike entity classes. Instances of an embeddable class share the identity of the entity that owns it.It exist only as the state of another entity.
In general, such a class serves to make the definition of common attributes for several Entity, we can assume that JPA simply embeds into the Entity instead of an object of this class, the attributes it contains.


What requirements does JPA set for Embeddable classes?

Such classes must satisfy the same rules as the Entity classes, except that they do not have to contain a primary key and be marked with the Entity annotation.
The Embeddable class must be marked with the Embeddable annotation or described in the XML configuration file JPA.