JPA Interview Questions
If you are searching for the JPA Interview Questions, then you are in the right place because, In this article, we will discuss some of the important JPA Interview Questions that were generally asked.
So, without further ado, let's discuss the JPA Interview Questions.
JPA Interview Questions
1. What is JPA?
JPA is the acronym for Java Persistent API, a Java specification used to persist data between the Java object and the relational database. It acts as the link between the object-oriented domain models and relational database systems. It requires an implementation as it doesn't perform any function itself. JPA 1.0 is the first version of the Java Persistence API released in 2006.
2. What are the advantages of JPA?
The advantages of JPA are-
- Due to O/R mapping and Database access processing, user programming becomes easy.
- The price of creating the definition file is minimized by using annotations.
- JPA will reduce the interaction with the database.
- We can also merge the applications used with other JPA providers.
3. What is the difference between the JPA and Hibernate?
The difference between the JPA and Hibernate are as follows-
- Hibernate is one of the most popular open-source implementations of the latest specification of JPA (JPA 2.1).
- JPA describes rules and APIs only but Hibernate implements these descriptions.
- JPA defines the specification, and It is an API.
4. Define Entity.
An entity is a group of states associated with a single unit, or an entity is an application-defined object in the Java Persistence Library. It behaves as an object and becomes a principal constituent of the object-oriented paradigm.
5. What are the different properties of an entity?
The properties of an entity are following-
- Persistability: If an object is stored in the database and can be accessed anytime, then that object is called persistent.
- Persistent Identity: Each Entity is unique and represents an object identity in Java. Similarly, A object is represented as a persistent identity when stored in a database. This object identity is similar to the primary key in the database.
- Transactionality: Transactions are a fundamental part of persistence, and it is a set of operations that either fail or succeed as a unit.
- Granularity: Entities should not be primitives or built-in objects with a single-dimensional state.
6. What is the role of the Entity Manager in Java Persistence API?
The following roles of the Entity Manager in JPA are-
- An entity manager is responsible for managing an object referenced by an entity.
- Entity Manager is accountable for implementing the API and encapsulating all of them within a single interface.
- It is used for operations like reading, deleting, and writing an entity.
7. What is an Entity manager?
The EntityManager is responsible for implementing an API that manages the lifecycle of entity instances. They manage a set of entities that are defined by a persistence unit.
The EntityManager API is used to create and remove persistent entity instances and find entities by their primary key, and query over entities. A persistence unit defines the set of entities that a given EntityManager instance can manage.
8. What are the basic requirements that the entity class has?
An entity class must have the following requirements:
- It must have a no-argument constructor.
- The class can't be final.
- It must be annotated with @Entity annotation.
- It must implement a Serializable interface if the value passes an empty instance as a detached object.
9. What is object-relational mapping?
Object-relational mapping is a mechanism to develop and maintain a relationship between an object and the relational database. It can do so by mapping an object state into the database column. Object-relational mapping converts attributes of programming code into columns of the table.
10. List some ORM frameworks.
The following are the various frameworks that function on the ORM mechanism: -
11. What are the various directions of entity mapping?
The direction of mapping will be either unidirectional or bidirectional. In unidirectional mapping, we can map only one Entity to another entity. In contrast, in bidirectional mapping, we can map each Entity to another entity or refer to another entity.
12. What type of annotations are used for issuing elements of the Entity collections?
The OrderBy and OrderColumn annotations are used to establish the procedure for issuing elements of the entity collections.
13. What are the different types of entity mapping?
The different types of entity mapping are-
- One-to-one mapping: In this mapping, an instance of one Entity is associated with an instance of another entity that represents a single-valued association.
- One-To-Many mapping: In this mapping, an entity is associated with a collection of other entities that come into the collection-valued association category.
- Many-to-one mapping: A collection of entities can be associated with a similar entity in this mapping.
- Many-to-many mapping: This mapping is a collection-valued association where any number of entities can be associated with a collection of other entities.
14. What are the different types of objects that can be stored in the JPA collections mapping?
The different types of objects that JPA allows to store are-
- Basic Types
15. What is JPQL?
JPQL, an acronym for Java Persistence Query language (JPQL), is a part of the JPA specification that defines searches against persistence entities. It is an object-oriented query language that performs database operations on persistent entities. JPQL uses an entity object model instead of the database to operate the SQL queries. Here, the primary role of JPA is to transform JPQL into SQL. Hence, it provides an easy platform for developers to handle SQL tasks.
16. What are the essential features of JPQL?
The essential features of JPQL are-
- JPQL is simple and robust.
- JPQL is a platform-independent query language.
- The queries of JPQL can be declared statically into metadata or dynamically built-in code.
- JPQL can be used with any database such as MySQL or Oracle.
17. What type of Java collections can be used in JPA?
The following types of Java collections that are used to store multivalued entity associations and a group of objects are-
18. What is the primary purpose of cascading operations in JPA?
We can make it applicable if we apply any task to one Entity to its related entities using cascading operations.
19. What is Embeddable class?
An embeddable class is a class that can contain single embedded classes and collections of such classes that are not used by themselves, only as part of one or more Entity classes. Such classes are used as keys or map values. Each embedded class may belong to only one object of the Entity class at the runtime. Also, it cannot be used to transfer data between the objects of the Entity classes.
20. What is Mapped Superclass?
A mapped Superclass is an inherited class from which Entity may contain JPA annotations. However, such a class is not an Entity as it does not have to fulfill all the requirements set for Entity. Such class cannot be used in EntityManager or Query operations as it should be marked with the MappedSuperclass annotation or described in the XML file.
21. What are the different types of caches that are used in JPA?
The two kinds of caches are:
- first-level cache- caches data from a single transaction.
- second-level cache- caches data for more than one transaction.
22. What unique inheritance strategy is used in Hibernate but not in the JPA?
Hibernate has a unique inheritance strategy that is called implicit polymorphism.
23. What is a JPA metamodel?
The JPA Metamodel API provides the ability to examine the persistent object model and retrieve details on managed classes and persistent fields and properties.
24. What does the Basic annotation do?
Basic annotations are the simplest type of data mapping to a database column. Also, in the annotation parameters, we can specify the fetch field access strategy and whether this field is required or not.
25. What does the Access annotation do?
Access annotations are used to define the access type for an entity class,
26. What do you mean by Spring data repository?
It is a very important feature of JPA as it helps in reducing a lot of boilerplate code. It also decreases the chance of errors. This is the key abstraction that is provided using the Repository interface.
27. Can we perform the actual tasks like access, persist, and manage data with JPA?
No, we cannot perform actual tasks because JPA is only a Java specification.
28. What is PagingAndSortingRepository?
The PagingAndSortingRepository provides methods to retrieve entities using pagination and sorting. It extends the CrudRepository interface.
29. What is PlatformTransactionMangaer?
PlatformTransactionMangaer is the central interface in Spring's transaction infrastructure that extends TransactionManager.
30. What do you mean by Criteria API?
The Criteria API is a specification using Java programming language APIs that provides type-safe and portable criteria queries. Criteria API is just an alternative method for defining JPA queries. Criteria API is the most common way of constructing queries for entities and their persistent state. Criteria API defines platform-independent criteria queries written in Java programming language. It was introduced in JPA 2.0. The main purpose is to provide a type-safe way to express a question.
31. What are the differences between the JPA and JDO?
JPA (Java Persistence API) and JDO (Java Data Objects) are used for storing Java objects in databases. JDO is a more general specification if JPA is concentrated only on relational databases as it describes the Object-Relational Model for any possible bases and repositories.
32. Write the two types of fetch strategies that are used in JPA?
The two types of fetch strategies used in JPA are:
- LAZY – It loaded only during the first access to the field.
- EAGER – It loaded immediately.
33. What are the different callback methods that are used in JPA?
The different callback methods are-
34. What are the six types of locks described in the JPA specification?
JPA has six types of locks that are mentioned below:
You got the top JPA Interview Questions as we have discussed varieties of JPA Interview Questions.
After reading about the JPA Interview Questions, are you not feeling excited to read/explore more articles on various CMS Platforms? Don't worry; Coding Ninjas has you covered. See Java, JVM and Its architecture, Introduction to Java, Methods in Ruby, and Java Development Kit(JDK).
- Embedded C Interview Questions
- Maven Interview Questions
Do upvote our blogs if you find them helpful and engaging!