January 31, 2020

Part 2: Spring Data JPA Tutorial and Interview Questions

What is JPA?
JPA (Java Persistence API) is the specification of Java that is used to persist data between Java object and RDBMS(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 implements JPA specifications for data persistence.


Does JPA performs the actual task like access, persist and manage data?
No, JPA is just a specification. The ORM tools like Hibernate, iBatis, and TopLink implements the JPA specification and perform the tasks.

What is the ORM?
ORM or 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.

ORM converts attributes of programming code into columns of the table. With ORM we can handle various database operations easily such as insertion, updation, deletion etc.

What are the advantages of JPA?
  • The burden of interacting with the database reduces significantly by using JPA.
  • The user programming becomes easy by concealing the object-relational mapping and database access processing.
  • The cost of creating the definition file is reduced by using annotations.
  • We can merge the applications used with other JPA providers
  • Using different implementations can add the features to the standard Implementation which can later be the part of JPA specification.
What is the JPQL?
JPQL is the Java Persistence query language defined in JPA specification and it is used to construct the queries.

What are the different types of entity mapping?

  • 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 source entity can be mapped with at most one instance of the target entity.
  • 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-one mapping represents a single-valued association where a collection of entities can be associated with the similar entity. In the relational database, more than one row of an entity can refer to the same row of another entity.
  • 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.

ALSO READHibernate Interview Questions (Mapping Relations)

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.

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.

What are the constraints on an entity class?
The class must have a no-argument constructor.
The class can't be final.
The class must be annotated with @Entity annotation.
The class must implement a Serializable interface if value passes an empty instance as a detached object.

What is the role of Entity Manager in JPA?
The entity manager implements the API and encapsulates all of them within a single interface.
The entity manager is used to read, delete and write an entity.
An object referenced by an entity is managed by entity manager.

What is the purpose of cascading operations in JPA? What are the types of cascade supported by JPA?
If we apply any task to one entity then using cascading operations, we make it applicable to its related entities also.
  • PERSIST: if the parent entity is persisted then all its related entity will also be persisted.
  • MERGE: if the parent entity is merged, then all its related entity will also be merged.
  • DETACH: if the parent entity is detached, then all its related entity will also be detached.
  • REFRESH: if the parent entity is refreshed, then all its related entity will also be refreshed.
  • REMOVE: if the parent entity is removed, then all its related entity will also be removed.
  • ALL: In this case, all the above cascade operations can be applied to the entities related to the parent entity.
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).

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.

-K Himaanshu Shuklaa..

No comments:

Post a Comment