Prepare Interview

Exams Attended

Mock Exams

Make Homepage

Bookmark this page

Subscribe Email Address

JPA Interview Questions and Answers

Freshers / Beginner level questions & answers

Ques 1. What is JPA?

JPA (Java Persistence API) is a Java EE and Java SE specification that describes a management system for saving java objects to relational database tables in a convenient form.

Java itself does not contain JPA implementations, however, there are many implementations of this specification from different companies (open and not). This is not the only way to save java objects in databases (ORM systems), but one of the most popular in the Java world.

Is it helpful? Add Comment View Comments
 

Ques 2. What is JPA and its features?

Java Persistence API (JPA) is a collection of classes and methods to persistently store vast amounts of data in a database which is provided by the Oracle Corporation.
The Java Persistence API (JPA) is now here to simplify the developer's life. Developers can now use the JPA API to develop applications easily. Here are the features of JPA:
  • JPA supports pluggable, third-party persistence providers such as Hibernate and TopLink, etc.
  • JDK 11 annotations are fully supported.
  • Few java classes are required to develop persistence applications.
  • JPA application can run outside the container also. So, developers can use JPA capabilities in desktop applications also.
  • No need to write deployment descriptors. Annotations-based meta-data are supported in JPA applications.
  • Annotation defaults can be used in the model class, which saves a lot of development time.
  • Provides cleaner, easier, standardized object-relational mapping.
  • JPA supports inheritance, polymorphism, and polymorphic queries.
  • JPA also supports named (static) and dynamic queries.
  • JEB QL is a very powerful query language provided by JPA.
  • JPA helps you build a persistence layer that is vendor-neutral and any persistence provider can be used.
  • Many IDEs are also available to ease the development of JPA applications.
  • Some IDEs can generate the model and persistence code from the database schema.
  • JPA application can also be configured to generate database schema based on the persistence model.
  • It is also very easy to switch to the most performing persistence provider. You can easily move to any commercial persistence provider.

Is it helpful? Add Comment View Comments
 

Ques 3. Why to use JPA?

  • JPA is the standard, and standards are good!
  • Using JPA does not tie you to Hibernate.
  • JPA gives you most of the features of plain old Hibernate, except:
  • No criteria queries in JPA 2.0. Criteria query is a neat feature of Hibernate that constructs queries using Java-based combinators instead of the alternate query language, getting the benefit of IntelliSense and Eclipse's refactoring tools.
  • JPA doesn't have Hibernate's DeleteOrphan cascade type.
  • Delete Orphan is a useful annotation that directs Hibernate to delete entities in a collection if the parent is deleted, preventing orphaning.
  • JPA doesn't have an equivalent to Hibernate's ScrollableResults.

Is it helpful? Add Comment View Comments
 

Ques 4. What is ORM in JPA?

Mapping between database tables and java objects called ORM (Object Relational Mapping). JPA (Java Persistence API) provides and ORM facility for managing relational tables in Java applications. It is a specification and few implementations are like Hibernate, JDO, EJB, Toplink. By using JPA can be fetched data, insert, update etc.

Is it helpful? Add Comment View Comments
 

Ques 5. What is an Entity?

A class which should be persisted in a database it must be annotated with javax.persistence.EntitySuch a class is called Entity. 

An instances of the class will be a row in the person table. So, the columns in the person table will be mapped to the Person java object annotated as @Entity.
While insert, update or fetch record to or from the database we use entity as mapping with relational tables.

Is it helpful? Add Comment View Comments
 

Ques 6. What is the difference between persistence.xml and hibernate.cfg.xml?

When using JPA need persistence.xml and while using Hibernate API need hibernate.cfg.xml. When using JPA or Hibernate not needed both xmls, need the xml configuration file according to JPA or Hibernate.

Is it helpful? Add Comment View Comments
 

Ques 7. What is the difference between JPA and Hibernate?

Hibernate is one of the most popular open-source implementations of the latest specification (JPA 3.0). Even more likely the most popular, almost standard de facto.

That is, JPA only describes rules and APIs, and Hibernate implements these descriptions, however Hibernate (like many other JPA implementations) has additional features not described in JPA (and is not portable to other JPA implementations).

For solid differences, check JPA vs Hibernate.

Is it helpful? Add Comment View Comments
 

Ques 8. What are the two types of elements in Entity classes?

JPA indicates that it can work both with properties of classes (property), designed in the style of JavaBeans, or with fields (field), that is, class variables (instance variables). Accordingly, the type of access will be either property access or field access.

Is it helpful? Add Comment View Comments
 

Ques 9. What types of connections (relationships) between Entities?

There are four types of connections

  1. One-To-One: one-to-one connection, that is, one Entity object can be associated with no more than one object of another Entity
  2. One-To-Many: one-to-many connection, one Entity object can be associated with a whole collection of Entity
  3. Many-To-One: A relationship where one entity (column or set of columns) is/are referenced with another entity (column or set of columns) that contains unique values. In relational databases, these relations are applicable by using foreign key/primary key between tables.
  4. Many-To-Many: A relationship is a connection between two types of entities. In the case of a many-to-many relationship, both sides can relate to multiple instances of the other side. Note that it's possible for entity types to be in a relationship with themselves.

Bi-directional: A mapping is the most common way to model this relationship with JPA and Hibernate. It uses an attribute on the Order and the OrderItem entity. This allows you to navigate the association in both directions in your domain model and your JPQL queries.

Uni-directional: A mapping is an association between one persistence object and another one related persistence object. If one persistence object uses another and in back if other is not using the first persistence object then it becomes unidirectional.

Is it helpful? Add Comment View Comments
 

Ques 10. What is Persistent Fields?

  • If the entity class uses persistent fields, the Persistence runtime accesses entity-class instance variables directly.
  • All fields not annotated javax.persistence.Transient or not marked as Java transient will be persisted to the data store.
  • The object/relational mapping annotations must be applied to the instance variables.

Is it helpful? Add Comment View Comments
 

Ques 11. Is it possible to use JPA with NoSQL databases?

In general, the JPA specification says only about mapping java objects into relational database tables, but there are a number of implementations of this standard for NoSQL databases: Kundera, DataNucleus, ObjectDB, and a number of others. Naturally, not all specification-specific features for relational databases are transferred to NoSQL databases completely.

Is it helpful? Add Comment View Comments
 

Ques 12. What JPA requirements for Entity classes can you list?

1) Entity class must be annotated with Entity or described in the XML configuration file JPA,

2) Entity class must contain a public or protected constructor with no arguments (it can also have constructors with arguments),

3) Entity class must be a top-level class (top-level class),

4) Entity class cannot be an enum or interface,

5) Entity class cannot be the final class,

6) Entity class cannot contain final fields or methods if they participate in the mapping (persistent final methods or persistent final instance variables),

7) If an Entity class object is passed by value as a separate object (detached object), for example through a remote interface (through a remote interface), it must also implement a Serializable interface,

8) 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),

9) 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.

Is it helpful? Add Comment View Comments
 

Ques 13. What is Persistent Properties?

  • If the entity uses persistent properties, the entity must follow the method conventions of JavaBeans components.
  • JavaBeans-style properties use getter and setter methods that are typically named after the entity class's instance variable names.
  • For every persistent property of type Type of the entity, there is a getter method getProperty and a setter method setProperty.
  • If the property is a Boolean, you may use isProperty instead of getProperty. For example, if a Customer entity uses persistent properties and has a private instance variable called firstName, the class defines a getFirstName and setFirstName method for retrieving and setting the state of the firstName instance variable.

    The method signature for single-valued persistent properties are as follows:

    Type getProperty()
    void setProperty(Type type)

    The object/relational mapping annotations for persistent properties must be applied to the getter methods. Mapping annotations cannot be applied to fields or properties annotated @Transient or marked transient.

Is it helpful? Add Comment View Comments
 

Ques 14. Explain Life Cycle of a JPA Entity.

Key states that an entity might be in:

  1. New / Transient: An object is instantiated but not yet associated with an Entity Manager and has no representation in the database.
  2. Managed / Persisted.
  3. Detached: Detached entity objects are objects in a special state in which they are not managed by any EntityManager but still represent objects in the database. Detached objects are often returned from a persistence tier to the web layer where they can be displayed to the end user in some form. Changes can be made to a detached object, but these changes won't be persisted in the database until the entity is reassociated with a persistence context (the entity is merged back to an EntityManager to become managed again).
  4. Removed.

    - The merge method's major task is to transfer the state from an unmanaged entity (passed as the argument) to its managed counterpart within the persistence context.
    - Merge deals with both new and detached entities. Merge causes either INSERT or UPDATE operation according to the sub-scenario (on the one hand it is more robust, on the other hand, this robustness needn't be required.)
    - Persist always causes INSERT SQL operation is executed (i.e. an exception may be thrown if the entity has already been inserted and thus the primary key violation happens.)

Is it helpful? Add Comment View Comments
 

Ques 15. Insert a record mechanism using JPA. Provide an example.

@Override
@Transactional
public void create(Category entity) throws MeetingAppDAOException {
  try {
    logger.info("Enter - create()");
    super.create(entity);
    logger.info("Exit - create()");
  } catch (PersistenceException exception) {
    logger.error("create()::REASON OF EXCEPTION=" + exception.getMessage(), e);
  }
}

Is it helpful? Add Comment View Comments
 

Ques 16. What is the attribute of the Entity class in JPA terminology?

JPA indicates that it can work both with properties of classes (property), designed in the style of JavaBeans, or with fields (field), that is, class variables (instance variables). Both types of elements of the Entity class are called attributes of the Entity class.

Is it helpful? Add Comment View Comments
 

Most helpful rated by users:

Related differences

JDBC vs JPAJPA vs Hibernate

Related interview subjects

Core Java interview questions and answers - Total 306 questions
Tomcat interview questions and answers - Total 16 questions
Apache Wicket interview questions and answers - Total 26 questions
Java Applet interview questions and answers - Total 29 questions
JAXB interview questions and answers - Total 18 questions
JMS interview questions and answers - Total 64 questions
Log4j interview questions and answers - Total 35 questions
Struts interview questions and answers - Total 84 questions
RMI interview questions and answers - Total 31 questions
Apache Camel interview questions and answers - Total 20 questions
JDBC interview questions and answers - Total 27 questions
Java 11 interview questions and answers - Total 24 questions
JPA interview questions and answers - Total 41 questions
EJB interview questions and answers - Total 80 questions
GWT interview questions and answers - Total 27 questions
Kotlin interview questions and answers - Total 30 questions
Glassfish interview questions and answers - Total 8 questions
Google Gson interview questions and answers - Total 8 questions
JSP interview questions and answers - Total 49 questions
J2EE interview questions and answers - Total 25 questions
Apache Tapestry interview questions and answers - Total 9 questions
Java Swing interview questions and answers - Total 27 questions
Java Mail interview questions and answers - Total 27 questions
Hibernate interview questions and answers - Total 52 questions
JSF interview questions and answers - Total 24 questions
Java 8 interview questions and answers - Total 30 questions
Java 15 interview questions and answers - Total 16 questions
JBoss interview questions and answers - Total 14 questions
Web Services interview questions and answers - Total 10 questions
RichFaces interview questions and answers - Total 26 questions
Servlets interview questions and answers - Total 34 questions
Java Beans interview questions and answers - Total 57 questions
Spring Boot interview questions and answers - Total 50 questions
JUnit interview questions and answers - Total 24 questions
Spring Framework interview questions and answers - Total 53 questions
Java Design Patterns interview questions and answers - Total 15 questions
©2023 WithoutBook