JPA Interview Question Part -2

Q1. What is Object-Relational Mapping?

Object-relational mapping (ORM, ORM, and O/R mapping) in computer software is a programming technique for converting data between incompatible type systems in relational databases and object-oriented programming languages. We wrap the tables or stored procedures in classes in the programming language, so that instead of writing SQL statements to interact with our database, we use methods and properties of objects.

Q2. In JPA EntityManager why use persist() over merge() ?

Persist: Persist takes an entity instance and adds it to the context making that instance managed.

Merge: Merge creates a new instance of your entity, copies the state from the supplied entity, and then makes the new copy managed. Find an attached object with the same id and update it.

Q3. What are the properties of an entity?

Persistability: An object is called persistent if it is stored in the database and can be accessed anytime.

Persistent Identity: In Java, each entity is unique and represents an object identity. Similarly, when the object identity is stored in a database, then it is represented as a persistence identity. This object identity is equivalent to the primary key in the database.

Transactionality: A transaction is a set of operations that either fail or succeed as a unit. Transactions are a fundamental part of persistence.

Granularity: Entities should not be primitives, primitive wrappers, or built-in objects with a single-dimensional state.

Q4. What is the role of the Entity Manager in JPA?

An entity manager is responsible for the following roles in JPA. The entity manager has the role to manage an object referenced by an entity. It implements the API and encapsulates all of them within a single interface. It is used for operations like reading, deleting, and writing an entity.

Q5. What is the Spring data repository?

Spring data repository is a very important feature of JPA. It helps in reducing a lot of boilerplate code. Moreover, it decreases the chance of errors significantly. This is also the key abstraction that is provided using the Repository interface. It takes the domain class to manage as well as the id type of the domain class as Type Arguments.

Q6. What is the difference between JPA and JDO?

JPA 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. JDO, from an API point of view, is agnostic to the technology of the underlying datastore, whereas JPA is targeted to RDBMS datastores. 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.

Q7. How can we create a custom repository in Spring Data JPA?

Repository

PagingAndSortingRepository

CrudRepository

JpaRepository

QueryByExampleRepository

Q8. What is the difference between CrudRepository and JpaRepository interfaces in Spring Data JPA?

CrudRepository provides CRUD functions. PagingAndSortingRepository provides methods to do pagination and sort records.

JpaRepository provides JPA-related methods such as flushing the persistence context and deleting records in a batch.

Q9. What requirements does JPA set for Embeddable classes?

Such classes must satisfy the same rules as the Entity classes, except that they do not have to contain a primary key and be marked with the Entity annotation. The Embeddable class must be marked with the Embeddable annotation or described in the XML configuration file JPA.

Q10. 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.

Q11. What are the types of cascade supported by JPA?

PERSIST: In this cascade operation, if the parent entity is persisted then all its related entities will also be persisted.

MERGE: In this cascade operation, if the parent entity is merged, then all its related entities will also be merged.
DETACH: In this cascade operation, if the parent entity is detached, then all its related entities will also be detached.
REFRESH: In this cascade operation, if the parent entity is refreshed, then all its related entities will also be refreshed.
REMOVE: In this cascade operation, if the parent entity is removed, then all its related entities will also be removed.
ALL, In this case, all the above cascade operations can be applied to the entities related to the parent entity.

Q12. What is JPQL?

The 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 is used to perform database operations on persistent entities. Instead of the database table, JPQL uses an entity object model to operate the SQL queries. It is used to create queries against entities to store in a relational database.

Q14. What are the features of JPQL?

It is simple and robust.
It is a platform-independent query language.
JPQL queries can be declared statically into metadata or can also be dynamically built-in code.
It can be used with any database such as MySQL, or Oracle.

Q15. 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 platform-independent criteria queries, written in Java programming language.


No comments:

Post a Comment