JPA Interview Questions
A list of top frequently asked JPA interview questions and answers are given below:
1) What is the Java Persistence API?
The Java Persistence API (JPA) is the specification of Java that is used to persist data between Java object and relational database. JPA acts as a bridge between object-oriented domain models and relational database systems. As JPA is just a specification, it doesn't perform any operation by itself. It requires an implementation. Therefore, ORM tools like Hibernate, TopLink, and iBatis implements JPA specifications for data persistence. The first version of the Java Persistence API, JPA 1.0 was released in 2006 as a part of EJB 3.0 specification.
2) Does JPA performs the actual task like access, persist and manage data?
No, JPA is only a specification. The ORM tools like Hibernate, iBatis, and TopLink implements the JPA specification and perform these type of tasks.
3) What is the object-relational mapping?
The object-relational mapping is a mechanism which is used to develop and maintain a relationship between an object and the relational database by mapping an object state into the database column. It converts attributes of programming code into columns of the table. It is capable of handling various database operations easily such as insertion, updation, deletion, etc.
4) What are the advantages of JPA?
The advantages of JPA are given below.
5) What are the embeddable classes?
Embeddable classes represent the state of an entity but do not have a persistent identity of their own. The objects of such classes share the identity of the entity classes that owns it. An Entity may have single-valued or multivalued embeddable class attributes.
6) List some ORM frameworks.
Following are the various frameworks that function on ORM mechanism: -
7) What is the JPQL?
JPQL is the Java Persistence query language defined in JPA specification. It is used to construct the queries.
8) What are the steps to persist an entity object?
The following steps are performed to persist an entity object.
9) What are the steps to insert an entity?
We can easily insert the data into the database through the entity. The EntityManager provides persist() method to add records. The following steps are used to insert the record into the database.
10) What are the steps to find an entity?
To find an entity, EntityManger interface provides find() method that searches an element by the primary key. The following steps are used to find an entity in the record.
11) What are the steps to update an entity?
JPA allows us to change the records in the database by updating an entity. The following steps are to be performed to update the entity.
12) What are the steps to delete an entity?
To delete a record from the database, EntityManager interface provides remove() method. The remove() method uses the primary key to delete the particular record. The following examples are to be performed to delete an entity.
13) Insert a record mechanism using JPA?
14) What are the different directions of entity mapping?
The direction of a mapping can be either unidirectional or bidirectional. In unidirectional mapping, only one entity can be mapped to another entity, whereas in bidirectional mapping each entity can be mapped or referred to another entity.
15) What are the different types of entity mapping?
Following are the types of object-relational mapping: -
16) What is an orphan removal in mappings?
If a target entity in one-to-one or one-to-many mapping is removed from the mapping, then remove operation can be cascaded to the target entity. Such target entities are known as orphans, and the orphanRemoval attribute can be used to specify that orphaned entities should be removed.
17) Explain persistence life cycle of an object?
In persistence life cycle, the object lies in the following states: -
18) What are the different types of identifier generation?
Following are the types of id generation strategy required to specify with @GeneratedValue annotation: -
19) What is an entity?
The entity is a group of states associated together in a single unit. An entity behaves as an object and becomes a major constituent of the object-oriented paradigm. In other words, we can say that an entity is an application-defined object in the Java Persistence Library. Each entity is associated with the metadata which represents its information in the form of XML or annotation.
20) What are the properties of an entity?
Following are the properties of an entity that an object must have: -
21) What is the role of Entity Manager in JPA?
An entity manager is responsible for the following operations.
22) What are the constraints on an entity class?
An entity class must fulfill the following requirements:
23) What is the purpose of Java collections in JPA?
In JPA, Java collections are used to persist the object of wrapper classes and String.
24) What type of objects can be stored in the JPA collections mapping?
Following are the type of objects that JPA allows to store: -
25) What type of collections can be used in JPA?
To store multivalued entity associations and a collection of objects, following types of Java collections is used: -
26) What is the purpose of cascading operations in JPA?
If we apply any task to one entity then using cascading operations, we make it applicable to its related entities also.
27) What are the types of cascade supported by JPA?
Following is the list of cascade type: -
28) What is JPQL?
The Java Persistence Query language (JPQL) is a part of JPA specification that defines searches against persistence entities. It is an object-oriented query language which is used to perform database operations on persistent entities. Instead of the database table, JPQL uses entity object model to operate the SQL queries. Here, the role of JPA is to transform JPQL into SQL. Thus, it provides an easy platform for developers to handle SQL tasks. JPQL is an extension of Entity JavaBeans Query Language (EJBQL).
29) What are the features of JPQL?
Some of the essential features of JPQL are: -
30) What is the Criteria API?
The Criteria API is a specification that provides type-safe and portable criteria queries written using Java programming language APIs. It is one of the most common ways of constructing queries for entities and their persistent state. It is just an alternative method for defining JPA queries. Criteria API defines a platform-independent criteria queries, written in Java programming language. It was introduced in JPA 2.0. The main purpose behind this is to provide a type-safe way to express a query.