Top JPA Interview Questions (2020) | CodeUsingJava

Most frequently asked JPA Interview Questions

What is JPA?

  • The Java Persistence API (JPA) is a Java application programming interface specification that describes the management of relational data in applications using Java Platform, Standard Edition and Java Platform, Enterprise Edition.
  • Java Persistence API is a collection of classes and methods to store the large quantity of data into a database.

JPA and Hibernate

What is the difference between JPA and Hibernate ?

  • Hibernate is one of the most popular open source implementations of the latest specification (JPA 2.1).
  • JPA only describes rules and APIs and Hibernate implements these descriptions, but Hibernate has additional features that are not described in JPA.
  • JPA defines the specification. It is an API.
  • Hibernate provides additional features on top of JPA. But depending on them would mean a lock in to Hibernate.

What are the advantages of JPA ?

    The advantages of JPA are as follows:
  • The application development process is enhanced by ObjectDB which is built to support the JPA
  • It increases the developers 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.

advantages of JPA

What is Object-Relational Mapping ?

  • Object-relational mapping (ORM, O/RM, 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.
  • This creates, in effect, a "virtual object database" that can be used from within the programming language.
  • 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.

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

    Both persist and merge have unique purposes.
  • Persist takes an entity instance and adds it to the context making that instance managed.
  • Insert a new register to the database.
  • 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.

What are the different types of entity mapping ?

    Following are the types of object-relational mapping: -
  • One-to-one mapping: This mapping represents a single-valued association where an instance of one entity is associated with an instance of another entity. This means that one instance of source entity can be mapped with at most one instance of the target entity.
  • One-To-Many mapping: This mapping comes into the category of collection-valued association where an entity is associated with a collection of other entities. This means that the instance of one entity can be mapped with any number of instances of another entity.
  • Many-to-one mapping: This mapping represents a single-valued association where a collection of entities can be associated with the similar entity. This means that more than one row of an entity can refer to the same row of another entity.
  • Many-to-many mapping: This mapping represents a collection-valued association where any number of entities can be associated with a collection of other entities. This means that more than one row of one entity can refer to more than one row of another entity.

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 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 single dimensional state.

  • What is the role of 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 read, delete and write an entity.