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
|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.|
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
- Top Link
5. Difference between Persist and Merge in JPA
|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?
- 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?
- 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.