Home  >  Blog  >   Java  > 

JPA Interview Questions

Do you want to get hired as a Java Developer or Full Stack Developer? Is your job interview date approaching? Are you anxious about handling JPA questions that could be asked in your interview? Do you want to ensure that you have practised enough JPA Interview Questions? Well, we are here to assist you. We have brought you a compilation of hand-picked JPA Interview Questions that could be asked in your interview.

Rating: 4.5
  
 
780

JPA stands for Jakarta Persistence API (formerly known as Java Persistence API). It is the Object Relational Mapping specification of Java. And this is used to store, manage, and access objects of Java in relational databases and databases such as NoSQL. JPA is neither a tool nor a framework. Rather, it's a specification of Java that allows the specific Java objects to be persisted. Java Persistence API's first version- JPA 1.0, was released in 2006. It was a part of the EJB (Enterprise JavaBeans) 3.0 specification. And since then, several versions have been released. In 2019, JPA got adopted as an independent Jakarta EE project.

Given below are some useful facts and insights on Jakarta Persistence API:
As per Statista reports, Java was among the top 5 most used languages in 2021, with a market share of 35.5% around the world.
The average salary of a Java Developer in the USA is around $57.75 per hour or $112,605 per annum. The salary ranges from the entry-level salary of $95,022 per annum to $146,250 per annum for experienced professionals.

Now that we have acquired basic knowledge and background of JPA. Then we will move to the JPA Interview Questions - updated (2022) for each of the following two sections separately:

Top 10 JPA Interview Questions

  1. What do you mean by the Entity in JPA?
  2. List some JPA requirements for entity classes.
  3. What are the embeddable classes?
  4. What are the main functions of EntityMangaer?
  5. List the types of connections available between the entities.
  6. What are the effects on the statuses of Entity objects of the merge operation?
  7. Mention the types of cache in JPA and their use.
  8. What is the purpose of Basic annotation?
  9. List the six types of locks available in JPA specification in ascending order.
  10. What is polymorphism in JPQL queries?

JPA Interview Questions For Freshers:

1. What do you mean by the Entity in JPA?

The Entity is a persistent domain object which is lightweight. The Entity is the main program entity. And additional classes can also be used by it, which can further be used to maintain the state of the Entity or auxiliary classes.
The entity class can inherit from other entity classes as well as the non-entity classes.

2. What is meant by the attribute of the entity class in the terminology of JPA?

There are two elements of JPA that it can work upon. First are the properties (property) of classes that are designed like that of JavaBeans. And the second is the fields (field) or the class variables. Both these elements- property and field are the attributes of the entity class.

Are you interested in taking up Core Java Certification Training? Enroll Now for Core Java Training

3. List some JPA requirements for entity classes.

  • An entity class should be described in the JPA's XML configuration file or annotated with the Entity.
  • It should be a top-level class.
  • It can neither be interface nor final class.
  • It should also contain a protected or public constructor without arguments.
  • The primary key should also be contained in an entity class.

4. What are the types of data that the entity class attributes allow?

The given attributes are valid in entity class:

  • Strings
  • Primitive types as well as their Java wrappers
  • Entity types
  • Enums
  • Embeddable classes
  • Java serializable types
  • Collection types 1 to 6

5. What are the embeddable classes?

An embeddable class cannot be used by itself. Both single embedded classes, as well as collections, can be contained in an entity class. We can use them as a map or key values also. Each embedded class belongs to just one object of the entity class at run time. And we can't use it for transferring data among the objects of entity classes. It serves the purpose of making definitions of common attributes for several entities.

 MindMajix YouTube Channel

6. List the valid attribute types that are included in the primary key of the entity class so that you can use them for any database.

The list of such attributes is given below:

  • Strings
  • Primitive types as well as their Java wrappers
  • Java.sql.Date and Java.util.Date 
  • BigInteger and BigDecimal

7. What are the requirements set by JPA for embeddable classes?

The following requirements are set by JPA:

  1. The embeddable class should be described in the JPA's XML configuration file or marked with embeddable annotation.
  2. The embeddable classes should satisfy the same rules as the entity classes. But they need not have to contain a primary key and are marked with the annotation of the Entity.

8. List the four lifecycle statuses of the life cycle of an entity instance.

The four lifecycle statuses of an entity object are given below:

New- The object is created. But the primary keys have not been generated, and the object hasn't been saved yet.
Managed- The object is created. Also, the primary keys have been generated, and it's managed by JPA.
Detached- The object is created, but the JPA doesn't manage it.
Removed- The object is created as well as managed by JPA, but it will get deleted after the transaction is committed.

9. What is an EntityManager?

An API is described by this EntityManager interface for Entity's all basic operations and also of other JPA entities.

10. What are the main functions of Entity Manager?

The main functions of EntityManger are given below:

  • For operations on the Entity
  • Data preparation
  • Preparation of other entities
  • Work with EntityGraph

11. What is meant by Mapped Superclass?

The Entity is inherited from this Mapped Superclass only. Because it is not exactly the Entity, it doesn't need to fulfil the requirements set for the Entity. We can't use this class in Query operations or EntityManager. And it must be described in an XML file and marked with the annotation of Mapped Superclass.

12. What three inheritance mapping strategies does the JPA describe?

The mapping strategies are the way of working with JPA with the entity-derived classes. The three types of inheritance strategies provided are:

  • Unifying strategy (the strategy of a joined subclass)
  • One table for the entire inheritance hierarchy
  • One table for each class

13. List the types of connections available between the entities.

The following four types of connections are available:

  • OneToOne: An object of the Entity can be associated with only one object of any other entity.
  • OneToMany: An object of the Entity can be associated with more than one object of other another entity.
  • ManyToOne: More than one object of an entity can be associated with another entity object.
  • ManyToMany: More than one object can be associated with another entity's more than one object.

14. Which two kinds of fetch strategies are available in JPA?

The following two types of fetch strategies are available in JPA

  • EAGER: these fields get loaded immediately.
  • LAZY: these fields can be only loaded on this field's first access.

15. Can some other embeddable class be contained in the embeddable class?

Yes maybe.

JPA Interview Questions For Experienced

1. On each of the four statuses of Entity objects, how are the operations persisted?

  • For the new status, the object will get saved to the database after a transaction is committed and changes to the status managed.
  • If the status is managed, it ignores the operation. But the status of the dependent Entity can be changed to 'managed' if the cascading annotations are there.
  • For the removed status, it changes to managed thereafter.
  • For the detached status, it throws the exception at the transaction's commit stage or right away.

2. What are the effects on the statuses of Entity objects of the merge operation?

  • For the new status, a new managed entity is created, and the past data will be copied into it.
  • For the managed status, it ignores the operation, but it works on the Entity dependent on cascading when the status is not managed.
  • For the removed status, it throws the exception at the transaction's commit stage or right away.
  • For the detached status, it either creates a new managed with the data copied or copies the data to the existing managed Entity

3. What are the effects on the statuses of Entity objects of the remove operation?

  • For the new status, it ignores the operation, but the status changes to removed on the Entity dependent on cascading. Here the status shouldn't be managed.
  • For the managed status, the status changes to removed thereafter, and the removed database records the object during the commit stage.
  • For the removed status, it ignores the operation.
  • For the detached status, it throws the exception at the transaction's commit stage or right away.

4. What are the effects on the statuses of Entity objects of the detach operation?

  • For the new or detached status, it ignores the operation.
  • For the managed or removed status, the Entity's status and also of the objects dependent on cascade become detached.

5. What are the effects on the statuses of Entity objects of the refresh operation?

  • For the new, detached, or removed status, it throws out the exception.
  • For the managed status, it restores all the changes from the database and refreshes all objects dependent on the cascade.

6. Mention the types of cache in JPA and their use.

There are two types of cache in JPA:

  • First-level cache: Data is cached from a single transaction.
  • Second-level transaction: Data is cached from more than one transaction

7. What is the purpose of access annotation?

The access type for an entity class, embeddable, superclass, or individual attributes is defined by it. This defines the way JPA refers to entity attributes such as class properties or class fields having setters and getters.

8. What is the purpose of Basic annotation?

The simplest type of mapping of data on a column of the database is indicated by the basic annotation. Also, the fetch field access strategy, as well as the requirement of the field, can be specified in the annotation parameters.

9. List the six types of locks available in JPA specification in ascending order.

The six types of locks are listed below in the ascending order of their reliability:

  • NONE
  • OPTIMISTIC
  • OPTIMISTIC_FORCE_INCREMENT
  • PESSIMISTIC_READ
  • PESSIMISTIC_WRITE
  • PESSIMISTICI

10. What options does JPA provide to set second-level cache, or what values can be taken from the persistence.xml by the shared-cache-mode element?

The following five options are there:

  • ALL
  • NONE
  • ENABLE_SELECTIVE
  • DISABLE_SELECTIVE
  • UNSPECIFIED

11. How can the JPA metadata that is the information about entity type, embeddable class, etc., be obtained?

The Metamodel interface is used by JPA for this purpose. We can obtain this interface's object using the method of getMetamodel from an EntityManger or EntityMangerFactory.

12. What is a unique inheritance strategy not present in the JPA specification but present in Hibernate?

The implicit polymorphism strategy is present in Hibernate but not in JPA.

13. What do you mean by JPQL (Java Persistence query language), and how does it differ from SQL?

JPQL is almost similar to SQL, but the names of entity classes and the attributes are used instead of the database columns. Also, the data types of entity attributes are used by the query parameters instead of database fields. And there is automatic polymorphism in JPQL, unlike SQL. KEY, TREAT, VALUE, MAP, and ENTRY are some of the unique functions of JPQL.

14. What is polymorphism in JPQL queries?

This means that regardless of the inheritance strategy, the objects of all the descendant classes are returned in each entity request.

15. How can the polymorphism be turned off in JPQL?

We can use the TYPE function in the where condition for turning polymorphism off in JPQL.

Frequently Asked JPA Interview Questions:

1. List some major companies that use Java.

Several major companies use Java around the world, such as:

  • Amazon
  • Google
  • Microsoft
  • LinkedIn
  • Uber Technologies Inc
  • Airbnb
  • PayPal

2. What are the popular alternatives to Java?

Some of the most popular alternatives to Java are given below:

  • Python
  • C language
  • Kotlin
  • Abstract
  • JavaScript
  • Golang
  • Scala
  • Java EE

3. List the main advantages of using JPA.

The main advantages of using JPA are given below:

  • The database access processing and concealing of O/R mapping make user programming easy.
  • JPA reduces the burden significantly of database interaction.
  • Applications that are used with other providers of JPA can be merged.
  • The annotations reduce the cost of the definition file's creation.
  • Features can be added to the standard implementation with the help of different implementations. And they can be part of the JPA specification later.

4. Are the tasks of accessing, managing, and persisting data performed actually by JPA itself?

No, JPA is just a specification. These tasks are performed, and the JPA specification is implemented by the ORM tools, including iBatis, Hibernate, and TopLink.

5. Mention some Object-Relational Mapping frameworks.

Some of the object-relational mapping frameworks are given below:

  • TopLink
  • JPOX
  • iBatis
  • Hibernate
  • ORMLite

6. What is meant by object-relational mapping?

The object-relational mapping is a process that we use for developing and maintaining the relationship between the relational database and an object. This is achieved by mapping into the database column an object. The attributes of programming code get converted into the table columns by these object-relational mappers. Various database operations like updating, inserting, deleting, etc., can be managed by these mappers.

7. Differentiate between Hibernate and JPA.

Hibernate falls under the category of most popular open-source implementations with the latest specification-JPA 2.1. Not only popular, but it is also considered the standard implementation. On the other hand, the task of JPA is describing the rules and APIs while Hibernate implements these descriptions. Just like several other implementations, Hibernate also has some additional features that are not described in JPA.

8. Can JPA be used with the NoSQL databases?

Generally, the java objects mapping into only relational databases are mentioned. Still, several NoSQL databases can be implemented, such as DataNucleus, Kundera, and ObjectDB, among others. Originally, all the specification-specific features could not be transferred completely to the NoSQL databases.

9. Differentiate between JDO and JPA.

JDO and JPA both are the specifications to store objects of Java in databases. JDO is a more general specification describing the object-relational mapping for all possible repositories and bases.
Although the API of JDO and JPA don't match, JPA can be viewed as a component of JDO that is meant for relational databases mainly.
While JPA is still developed by JSR, JDO is now developed by the Apache JDO project.

10. What are the core concepts of JPA?

The core concepts of JPA are listed below:

  • Entity
  • Field persistence
  • Relationships
  • Entity manager

Conclusion:

Java is one of the most used languages all around the world. And the Jakarta Persistence API (JPA) enhances its usability even further. This makes it a valuable skill to gain. And hence, a vast pool of opportunities is available in this field. We hope this JPA Interview Questions compilation can help you get the most out of this opportunity available and crack your interview. 

Join our newsletter
inbox

Stay updated with our newsletter, packed with Tutorials, Interview Questions, How-to's, Tips & Tricks, Latest Trends & Updates, and more ➤ Straight to your inbox!

Course Schedule
NameDates
Core Java TrainingNov 05 to Nov 20
Core Java TrainingNov 08 to Nov 23
Core Java TrainingNov 12 to Nov 27
Core Java TrainingNov 15 to Nov 30
Last updated: 01 November 2022
About Author
SaiKumar Kalla

Kalla Saikumar is a technology expert and is currently working as a content associate at MindMajix. Write articles on multiple platforms such as ServiceNow, Business Analysis, Performance Testing, Mulesoft, Oracle Exadata, Azure, and other courses. And you can join him on LinkedIn.

Recommended Courses

1 /15