JPA Interview Questions

Q1. What is the JPA? Define JPA.

It stands for Java Persistence API. It is the specification of Java that is used to persist data between Java objects and relational databases. Java Persistence API is a collection of classes and methods to persistently store huge amounts of data into a database which is provided by the Oracle Corporation. JPA acts as an intermediate between object-oriented domain models and relational database systems. It requires an implementation. Therefore, ORM tools like Hibernate JPA specifications for data persistence.


Q2. What is the difference between JPA and Hibernate?
Hibernate is one of the most popular open-source implementations of the latest specification JPA. JPA is described in javax.persistence package. Hibernate is described in org.hibernate package. JPA only describes rules and APIs. Hibernate implements these descriptions, however. In other words, we can say hibernate is the implementation of JPA.


Q3. Does JPA perform the actual task like accessing, persisting, and managing data?

No, JPA is only a specification. The ORM tool like Hibernate implements the JPA specification and perform these type of tasks.

Q4.  What is the difference between JPA and JDO?

JPA (Java Persistence API) and Java Data Objects (JDO) are two specifications for storing java objects in databases. If JPA is concentrated only on relational databases, then JDO is a more general specification that describes the ORM for any possible bases and repositories. In principle, JPA can be viewed as part of the JDO specification specialized in relational databases, even though the API of these two specifications does not completely match.


Q 5. What is an Entity?

The entity is a lightweight persistent domain object. 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. Each entity is associated with the metadata representing its information in the form of XML or annotation.

Q6. What JPA requirements for Entity classes?

The entity class must be annotated with Entity or described in the XML configuration file JPA. The entity class must contain a public or protected constructor. The entity class must be a top-level class. Entity class cannot be enum or interface. The Entity class fields should be directly accessible only to the methods of the Entity class and should not be directly accessible to other classes using this entity. Such classes should refer only to methods (getter/setter methods or other business logic methods in the Entity class) Entity class cannot be the final class. Entity classes cannot contain final fields or methods if they participate in the mapping. The Entity class must contain a primary key, that is, an attribute or group of attributes that uniquely defines the record of this Entity class in the database.

Q7. 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 own them. An Entity may have single-valued or multivalued embeddable class attributes. An embeddable class is a class that is not used by itself, only as part of one or more Entity classes.


Q8. List some ORM frameworks.

1. Hibernate
2. TopLink
3. ORMLite
4. iBATIS


Q9. What types of relationships are between entities?

Four types of relationships between entities are->

One-to-one mapping: The one-to-one mapping represents a single-valued association where an instance of one entity is associated with an instance of another entity. In this type of association, one instance of the source entity can be mapped with at most one instance of the target entity.

One-To-Many mapping: The One-To-Many mapping comes into the category of collection-valued association where an entity is associated with a collection of other entities. In this type of association, the instance of one entity can be mapped with any number of instances of another entity.

Many-to-many mapping The Many-To-Many mapping represents a collection-valued association where any number of entities can be associated with a collection of other entities. In the relational database, more than one row of one entity can refer to more than one row of another entity.

Many-to-one mapping The Many-To-One mapping represents a single-valued association where a collection of entities can be associated with a similar entity. In the relational database, more than one row of an entity can refer to the same row of another entity.


Q10. What is Mapped Superclass?

A mapped Superclass is a class from which Entity is inherited, it may contain JPA annotations, however, such a class is not Entity, it does not have to fulfill all the requirements set for Entity. Such a class cannot be used in EntityManager or Query operations. Such a class should be marked with the MappedSuperclass annotation or, respectively, described in the XML file.


Q11. What are the two types of fetch strategies in JPA?
JPA describes two types of fetch strategies:

LAZY – these fields will be loaded only during the first access to this field,

EAGER – these fields will be loaded immediately.


Q12. What is EntityManager?

EntityManager is an interface that describes an API for all basic operations on the entity, data retrieval, and other JPA entities. It is the main API for working with JPA.

Q13. What is the four lifecycle status of an Entity Instance’s Life Cycle?
An Entity object has four lifecycle statuses: new, managed, detached, or removed.

new – the object has been created but has not yet generated primary keys and has not yet been saved in the database.

managed – the object has been created, managed by JPA, and has generated primary keys.

detached – the object has been created, but not managed (or no longer managed) JPA.

removed – the object is created, and managed by JPA, but will be deleted after the commit transaction.


Q14. How does the operation persist on Entity objects of each of the four statuses?

If the status is Entity new, then it changes to managed and the object will be saved to the database when a transaction is committed or as a result of flush operations. If the status is already managed, the operation is ignored, but the dependent Entity can change the status to manage if there are annotations of cascading changes. If the status is removed, then it changes to manage. If the status is detached, the exception will be thrown right away or at the commit stage of the transaction.


Q15. What are the advantages of JPA?

The advantages of JPA are given below.

The burden of interacting with the database reduces significantly by using JPA. The user programming becomes easy by concealing the O/R mapping and database access processing. The cost of creating the definition file is reduced by using annotations. It increases the developer's productivity as the code for mapping the database tables and the domain models are written once and can be used for other operations. It is database-independent. JPA provides an independent abstraction layer on top of SQL.



No comments:

Post a Comment