Top JPA Interview Questions and Answers

jpa-interview-questions

If you are looking for JPA interview questions and answers for experienced and freshers, you are at the right place. Many companies are looking for JPA experts, and there are a lot of opportunities as a JPA developer. So you have an excellent chance to move ahead in your career. Our top 20 JPA interview Questions will help you to crack the interview and acquire your dream job.

JPA Interview Questions and Answers

1 Explain about JPA?

Ans: (JPA)Java Persistence API is a specification of java that collects the data between relational databases and java objects. It acts as a bridge between relational database systems and object-oriented domains. JPA requires implementation as it is specified because it does not perform any implementation itself. Therefore ORM tools like TopLink, iBatis, and Hibernate show JPA specifications for data persistence. 

2 What is Object-Relational Mapping?

Ans: Object-Relational Mapping term is a mechanism which maintains and develops a relationship between relational databases and an object by mapping database columns into an object state. It transforms characteristics of programming code into columns of the table. It is capable of managing diverse database operations efficiently, such as update, insertion, deletion, etc.

3. What are the advantages of JPA?

Ans: Following are the JPA advantages:

  • Using JPA, it reduces the interaction with the database significantly. 
  • Enhance user programming easily by concealing the database access processing and O/R mapping.
  • It reduces the cost of creating the definition file by using annotations.
  • Using JPA providers, you can merge the applications. 
  • Different implementations can add the features to the standard Implementation, which can be a succeeding part of JPA specification.

4. Does JPA perform tasks like manage, access, and persist data?

Ans: JPA cannot perform the tasks like access, manage and persist data because it is only a specification. The ORM tools like iBatis, TopLink, and Hibernate implement the JPA specification and perform these types of tasks.

 

5. Differentiate between Hibernate and JPA?

Ans: JPA is an interface while Hibernate is implementation. They have various Java ORM solutions. 

  • TopLink
  • EclipseLink
  • Hibernate
  • JDO

6. Tell us about JPA Entity Class?

Ans: A JPA entity class is plain old Java Object such that it is a regular Java class, having the ability to represent the objects in the database. Conceptually this is related to serializable classes, which are having the capability to be serialized.

7. List out various ORM frameworks?

Ans: Here are different ORM frameworks that function on ORM systems. 

  • Hibernate
  • TopLink
  • ORMLite
  • iBATIS
  • JPOX

8. What is meant by JPQL?

Ans: JPQL term is an acronym of Java Persistence query language which is defined in Java specification. It is used to construct the queries against entities that are stored in a relational database. Using Select, update and delete clauses, JPQL can retrieve, update and delete the data.

9. What are the core classes and interfaces of JPA API or Explain about JPA Architecture?

Ans: The below image shows you classes levels and interfaces of JPA architecture.

EntityManagerFactory: It is a factory class of EntityManager which manages and creates various EntityManager instances. 

EntityManager: It acts as an interface and manages the persistence of an object. EntityManager works like a factory of Query instances. 

Entity: These are persistence objects and stores as records in the database. 

EntityTransactions: EntityManager has a one-to-one relationship with EntityTransaction. For every EntityManager operations are maintained by EntityTransaction class. 

Persistence: This class consists of static methods to obtain EntityManagerFactor instances. 

Query: This interface is implemented by each JPA vendor to obtain relational objects that meet the criteria.

10. What are the features of JPQL?

Ans: Following are some of the important features of JPQL are: –

  • It is robust and simple.
  • Platform-independent query language.
  • JPQL queries are declared dynamically built-in code, or it is declared statically into metadata 
  • It can be utilized with any database such as MySQL, Oracle.

11. Explain the criteria of API?

Ans: The Criteria API is a term that provides type-safe and portable query written using Java programming language APIs. It is one of the most traditional ways of creating queries for entities and their persistent state. And also it is defined as platform-independent criteria queries, written in Java programming language. 

12. Explain What type of objects can be stored in the JPA collections mapping?

Ans: Here are the various types of objects that JPA allows to store and map the data. 

  • Basic Types
  • Entities
  • Embeddable

13. Explain what type of collections can be used in JPA?

Ans: Following are the various types of collections that can be used to store multivalued entity associations and a collection of objects. 

  • List
  • Set
  • Map

14. Explain the purpose of Java collections in JPA?

Ans: In JPA, Java collections are used to persist the object of wrapper strings and classes. 

15. Explain various constraints on an entity class?

Ans: An entity class must satisfy the following requirements:

  • 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 the value passes an empty instance as a detached object.

16. Explain the roles of EntityManager in JPA?

Ans: An entity manager is responsible for the following actions.

  • The entity manager executes the API and encapsulates all of them within an interface.
  • The entity manager is used to write, delete and read an entity.
  • The entity manager manages an object referenced by an entity.

17. Explain about an entity?

Ans: The entity is a group of states that are associated together in a single unit. An entity behaves like an object and becomes a main constituent of the object-oriented standard. In other words, we can say that an entity is an application that defines the object in the Java Persistence Library. Each entity is associated with the metadata which represents its data in the form of XML or annotation.

18. Explain various types of identifier generation?

Ans: Following are various types of id generation strategy required to specify with @GeneratedValue annotation: –

  • Automatic Id generation – In this instance, the application doesn’t require any id generation and can hand over this task to the provider. If any value is not specified explicitly, the generation type defaults to auto.
  • Id generation using a table – The identifiers generates by using a database table.
  • Id generation using a database sequence – Databases support an internal mechanism for generating an id that is called sequences. To customize the database sequence name, we can use the JPA @SequenceGenerator annotation.
  • Id generation using a database identity – To generate identifiers for the object, every row is inserted into the table, and a unique identifier is assigned. 

19. Explain the persistence life cycle of an object?

Ans: In persistence life cycle, the object lies in the following phases: –

  • Transient – In this phase, the object will be in a transient state when a new keyword is declared, and it does not contain any primary key in the database. 
  • Persistence – In this phase, the object is associated with the session, and it is retrieved from the database. It remains in the persistence state when it contains a row of the database and consists of an identifier value. 
  • Detached – hibernate session is closed when the object enters into a detached state. The amends made to the detached objects are saved to the database.

20. What is an orphan removal in mappings?

Ans: If a target entity in one-to-many or one-to-one mapping is removed from the mapping, such target entities are known as orphans. The orphan removal attribute can be used to specify that orphaned entities should be removed.