We’ve been reading blogs and watching videos to compile a list of the most frequently asked JPA Interview Questions. This will allow you to easily pass the interview.

1) What is JPA?

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

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

2) Difference between Hibernate vs JPA?

It can be the highest priority in the list of interview questions on JPA. Just to start with the introduction we have kept this on the second number.

Hibernate is one of the most popular open implementations of the latest version of the specification (JPA 2.1). Even more likely the most popular, almost standard de facto. That is, JPA only describes rules and APIs, and Hibernate implements these descriptions, although Hibernate (as well as many other JPA implementations) has additional features not described in JPA (and not portable to other JPA implementations).

3) Can I use JPA with NoSQL databases?

In general, the JPA specification only refers to the mapping of Java objects to 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, in this case, not all relational database-specific features of the specification are transferred to the NoSQL database completely.

4) What is the difference between JPA and JDO?

JPA (Java Persistence API) and Java Data Objects (JDO) are two specifications for saving java objects in databases. If JPA is concentrated only on relational databases, then JDO is a more general specification that describes ORM for any possible databases and repositories. 

In principle, you can view JPA as a specialized part of the JDO specification for relational databases, even though the API of these two specifications does not completely match. Also, the “developers” of specifications are different – if JPA is developed as JSR, then JDO was first developed as JSR, now it is developed as an Apache JDO project.

5) What is an Entity?

The entity is a lightweight persistent domain object. The main program entity is an entity class, which can also use additional classes that can be used as helper classes or to preserve the state of the entity.

6) Can Entity be an abstract class?

Maybe, in doing so, it saves all Entity properties, except that it can not be directly initialized.

7) What JPA requirements for Entity classes you can list (at least six requirements)?

  1. The Entity class must be marked with the Entity annotation or described in the XML configuration file of the JPA,
  2. The Entity class must contain a public or protected constructor with no arguments (it can also have constructors with arguments),
  3. The Entity class must be a top-level class (top-level class),
  4. Entity class cannot be enum or interface,
  5. ntity class cannot be a 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 the Entity object of the class is passed by value as a detached object, for example through the remote interface, it must also implement the Serializable interface,
  8. Entity fields must be directly accessible only to the methods of Entity itself class and should not be directly accessible to other classes that use this Entity. Such classes should only refer to methods (getter/setter methods or other methods of business logic in the Entity class),
  9. The Entity class must contain the primary key, that is, the attribute or attribute group that uniquely identifies the record of this Entity class in the database.

8) What are the two types of elements that Entity classes have? Or in other words, list two kinds of access to the Entity classes.

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

9) What is the Entity attribute of the class in the terminology of JPA?

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

10) What types of data are allowed in the Entity attributes of the class (fields or properties)?

Valid types of attributes for Entity classes are:

  1. Primitive types and their Java wrappers,
  2. Strings,
  3. Any Java serializable types (implementing Serializable interface),
  4. enums;
  5. entity types;
  6. embeddable classes
  7. and collections of types 1-6

10) What is an Embeddable class?

An Embeddable class is a class that is not used by itself, only as part of one or more Entity classes.

Entity classes can contain both single embedded classes and collections of such classes. At runtime, each embedded class belongs to only one Entity class object and cannot be used to transfer data between Entity classes (that is, this class is not a common data structure for different objects).

In general, this class serves to render the definition of common attributes for several Entities; we can assume that JPA embeds merely in Entity instead of the object of such class those attributes that it contains.

What types of relations (relationship) between Entity you know (list eight types, or specify four types of links, each of which can be divided into two more types)?

There are the following four types of links:

  1. OneToOne (one-to-one relationship, that is, one Entity can be associated with no more than one object of another Entity),
  2. OneToMany (one-to-many relationship, one Entity can be associated with a whole collection of other Entity),
  3. ManyToOne (many-to-one communication, feedback for OneToMany),
  4. ManyToMany (many-to-many relationship) Each of which can be divided into two more types:

11) What is Mapped Superclass?

Mapped Superclass is a class from which Entity is inherited, it can contain JPA annotations, but the class itself is not Entity, it does not have to fulfill all the requirements set for Entity (for example, it may not contain a primary key).

This class cannot be used in EntityManager or Query operations. This class should be marked with the MappedSuperclass annotation or, respectively, described in the XML file.

12) What are the three types of Inheritance Mapping Strategies described in JPA?

In JPA, three Inheritance Mapping Strategies are described, that is, how JPA will work with Entity class inheritance classes:

  1. One table for the entire hierarchy of inheritance:
    • All entities, with all heirs recorded in one table, the special column “discriminator column” is defined to identify the entity type. For example, if there are an Entity Animals with the Cats and Dogs descendant classes, with this strategy, all the entities are written to the Animals table, but they have an additional animal-type column in which the value cat or dog is respectively written.
    • The minuses’ that in the common table, all fields unique for each of the descendant classes that will be empty for all other child classes will be created. For example, in the table of animals, there will also be a speed of climbing a tree from cats and whether a dog can bring sneakers from dogs that will always have a null for dog and cat, respectively.
  2. Joined subclass strategy – 
    • In this strategy, each entity class stores data in its table, but unique columns (not inherited from the ancestor classes) and the primary key, and all legacy columns are written to the ancestor class tables, additionally the relationships between these tables are established, for example, in the case of the Animals classes (see above), there will be three tables of animals, cats, dogs, and in cats only the key and the speed of the lasagna will be recorded, in dogs – the key and whether the dog can bring a stick , and in animals all other data cats and dogs clink to the appropriate table. The downside here is the performance loss from joining the tables for any operations.
  3. One table for each class – 
    • Here everything is simple, each separate class-heir has its table, i.e. for cats and dogs (see above), all data will be written merely into the cats and dogs tables as if they did not have a common superclass at all. The downside is the poor support of polymorphic relationships and the fact that sampling all classes of the hierarchy will require a large number of individual SQL queries or the use of a UNION query.

13) What are the two types of fetch strategies in JPA you know? One of the frequently asked JPA interview question.

JPA describes two types of fetch strategies:

  • LAZY – the field data will be loaded only during the first access to this field,
  • EAGER – the field data will be loaded immediately.

14) What is an EntityManager and what are its primary functions you can list?

EntityManager is an interface that describes the API for all the basic operations on Entity, getting data and other JPA entities. The main API for working with JPA. Basic operations:

  • For operations on Entity: persist (adding Entity for JPA control), merge, remove, refresh, detach (removal from JPA control), lock (blocking Entity from changes in other thread),
  • Get data: find (find and get Entity), createQuery, createNamedQuery, createNativeQuery, contains, createNamedStoredProcedureQuery, createStoredProcedureQuery
  • Get other JPA entities: getTransaction, getEntityManagerFactory, getCriteriaBuilder, getMetamodel, getDelegate
  • Working with EntityGraph: createEntityGraph, getEntityGraph
  • General operations on EntityManager or all Entities: close, isOpen, getProperties, setProperty, clear.

15) How does the persist operation affect Entity objects of each of the four statuses?

  1. If the status is a new Entity, it changes to manage, and the object will be saved to the database when the transaction is committed or as a result of flush operations,
  2. If the status is already managed, the operation is ignored, but the dependent Entity can change the status to manage if they have annotations for cascading changes,
  3. If the status is removed, it changes to manage,
  4. If the status is detached, an exception will be thrown immediately or at the commit stage of the transaction.

16) How does the remove operation affect the Entity objects of each of the four statuses?

  1. If the status is Entity new, the operation is ignored, however, dependent Entity can change the status to remove if they have annotations of cascading changes and they had the status managed
  2. If the status is managed, then the status changes to removed, and the object in the database will be written deleted when the transaction is committed (also remove operations for all cascade dependent objects);
  3. If the status is removed, the operation is ignored;
  4. If the status is detached, an exception will be thrown immediately or at the commit stage of the transaction.

17) How does the refresh operation affect Entity objects of each of the four statuses?

  • If the status is Entity managed, then the operation will restore all changes from the database of this Entity, also refresh all cascade-dependent objects,
  • If the status is new, removed or detached, will be thrown exception.

18) How does the detach operation affect the Entity objects of each of the four statuses?

1) If the status is Entity managed or removed, then the operation status of Entity (and all cascade-dependent objects) becomes detached.

2) If the status is new or detached, the operation is ignored.

Conclusion

We hope this compiled list of interview questions on JPA will help you to land in a good company. If anything is missing or needs to be corrected. Provide your suggestions in the comment section.

69 / 100