Top 20 JPA Interview Questions [Updated 2021]

Looking for frequently asked interview questions on Java Persistence API, we have curated the best JPA Interview questions for you to prepare before you attend an interview.

1. What is JPA?

Ans. JPA is a Java persistence API that provides a framework for storing and retrieving data in Java. It acts as a bridge between object-oriented domain models and relational database systems. It describes the management of relational data in applications using Java Platform, Standard Edition and Java Platform, Enterprise Edition.

JPA, as a specification, does not perform any operations on its own. It must be put into action. As a result, ORM tools such as Hibernate, TopLink, and iBatis support JPA specifications for data persistence.

The latest version of JPA is JPA 3.0. It was released in 2020 and had many new changes like

  • Java Persistence API is renamed as Jakarta Persistence API
  • Change of packages and properties from javax.persistence to jakarta.persistence.

2. State the differences between JPA and Hibernate

Ans.

JPA Hibernate
JPA is used to access, manage and persist data between Java object and relational database Hibernate is used to store Java objects in the relational database system

 

Considered as standard approach for ORM Is an open-source, lightweight, ORM tool
It is merely a specification. It is used for data persistence by a variety of ORM tools. It is one of the most popular JPA implementations.
It makes use of the Entity Manager interface to perform operations on instances of mapped entity classes such as creating, reading, and deleting them. This interface is used to communicate with the persistence context. It makes use of the Session interface to perform operations on instances of mapped entity classes such as creating, reading, and deleting them. It acts as a bridge between a Java application and hibernate at runtime.
To perform database operations, it employs Java Persistence Query Language (JPQL), an object-oriented query language. To perform database operations, it employs Hibernate Query Language (HQL), an object-oriented query language.

 

Related Page: Spring Boot Interview Questions

3. What is Object Relational Mapping (ORM)?

Ans. By mapping an object state into a database column, object-relational mapping is a mechanism for developing and maintaining a relationship between an object and a relational database. It converts attributes of programming code into table columns. It can easily handle various database operations such as insertion, updation, deletion, and so on.

4. Name few ORM Frameworks

Ans. The following are a few ORM Frameworks

  1. Top Link
  2. ORMLite
  3. iBatis
  4. JPOX

5. Difference between Persist and Merge in JPA

Ans.

Persist Merge
Persist should only be used on new entities to add them to the database (if entity already exists in DB there will be Entity Exists Exception throw). The merge operation is used to insert an entity into the context or to update information about an entity that is already managed, and the result is the managed entity.
In the database, insert a new register. Find and update an attached object with the same id.
It always creates a new entity and never updates an existing one. Alternatively, it throws and exception (Entity Exists Exception) due to a violation of primary key uniqueness. It inserts or updates a database entity.

 

6. What are embedded classes in JPA?

Ans.

  • An entity may contain references to non-entity classes. These non-entity classes are known as embeddable classes.
  • These classes’ objects share the identity of the entity classes that own them.
  • An Entity can have embeddable class attributes that are single-valued or multivalued. The embeddable class’s fields are all mapped to the owner entity table.
  • For a non-entity class, the @Embeddable annotation is used. This is the class that must be nested within an entity class.
  • Except for the @Entity annotation, the embeddable class and its fields/properties will use all annotations used by an entity (e.g., @Column, etc.).
  • The entity class makes use of @Embedded. This annotation is applied to a field/property of an embeddable class.
  • Embedded objects are solely the property of the entity that owns them and should not be shared with other persistent entities.

7. What are the different types of entity mapping?

Ans. Following are some of the different types of entity mapping

  • One-to-one Mapping: This mapping is a single-value association in which one entity’s instance is associated with another entity’s instance. This means that only one instance of the source entity can be mapped to one instance of the target entity.
  • One-To-Many Mapping: This mapping is a collection-valued association, which means that an entity is associated with a collection of other entities. This means that a single instance of one entity can be mapped to an infinite number of instances of another entity.
  • Many-To-One Mapping: This mapping represents a single-valued association in which a group of entities can be linked to a similar entity. This means that multiple rows of the same entity can refer to the same row of another entity.
  • Many-To-Many Mapping: This mapping represents a collection-valued association in which any number of entities can be linked to a group of other entities. This means that multiple rows of one entity can refer to multiple rows of another entity.

8. What is the role of Entity Manager in JPA?

Ans. In JPA, an entity manager is in charge of the following roles.

  • The entity manager is in charge of an object that is referenced by an entity.
  • It implements the API and encapsulates it all in a single interface.
  • It is used for operations such as reading, deleting, and writing an entity.

 9. What are the properties of an Entity?

Ans. The following are the properties of an entity:

  • Persistence: An object is said to be persistent if it is stored in a database and can be accessed at any time.
  • Persistent Identity: Each entity in Java is distinct and represents an object’s identity. Similarly, when an object’s identity is stored in a database, it is referred to as the persistence identity. This object identity corresponds to the database’s primary key.
  • Transactionality: A transaction is a group of operations that either succeed or fail as a whole. Transactions are an essential component of persistence.
  • Granularity: Entities should not be primitives, primitive wrappers, or built-in objects with a single dimension of state.

10. What is JPQL?

Ans. JPQL (Java Persistence Query Language) is a query language that is used to perform database operations on persistent entities. To execute SQL queries, JPQL employs the entity object model rather than the database table.

11. What is Mapped Superclass in JPA?

Ans. A mapped superclass is an abstract class that extends to a concrete entity in a relationship. A mapped superclass can be shared across entities of the same table, or shared between tables and managed by JPA.

12. What are the types of fetch strategies in JPA?

Ans. There are 3 fetch strategies

  • FetchType.LAZY
  • FetchType.EAGER
  • FetchType.FOLLOWING

13. What is an Embeddable class?

Ans. A class that has a primary key mapped by default is called Embeddable type class.

14. What are the different ways to create an entity?

Ans. There are 3 ways to create an entity

1. By annotating a class with @Entity annotation

2. Using PersistenceUtil:createEntity() method

3. Using PersistenceXmlfile:newInstance() method

15. What is a primary key in JPA?

Ans. A primary key for an entity is called the natural key. It cannot be changed. It identifies an entity uniquely.

16. What is a persistent Id?

Ans. Persistent id is an attribute of an entity that holds the object Serialization format. PersistenceId does not allow null values, it is very similar to a primary key in relational databases.

17. What is the difference between Embeddable and Attribute?

Ans. An entity type must have a primary key.  An attribute is not required to have one. An embeddable object should not have any persistent properties of its own but it may reference an entity or another embeddable type via foreign keys, relationships, or collections.

18. What are the two ways of persisting an object?

Ans. Below are the two ways of persisting an object:

  • By inserting into the database and using version field with @Version annotation
  • By Map with entity class and version field.

19. What does the @Entity annotation do?

Ans. It tells the JPA persistence provider that the class is a persistent entity that is to be tracked for changes.

20. How are entities mapped in JPA?

Ans. JPA doesn’t provide any schema, it uses annotations to manage the mapping between Java classes and database tables. Entities have an identifier property, that’s mapped by default as “id” in the database schema.

21. How does an entity get automatically persisted?

Ans. By default, every entity that has proper annotations gets automatically persisted when it is read from the EntityManager and will be removed from the persistence context when it is no longer referenced and flushed from the session cache.

Recent Articles

Related Stories

Leave A Reply

Please enter your comment!
Please enter your name here

Subscribe to get IQ's , Tutorials & Courses