Prepare Interview

Mock Exams

Make Homepage

Bookmark this page

Subscribe Email Address

JPA Interview Questions and Answers

Related differences

JDBC vs JPAJPA vs Hibernate

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 EntityManager?

  • Entity manager javax.persistence.EntityManager provides the operations from and to the database, e.g. find objects, persists them, remove objects from the database, etc.
  • Entities which are managed by an EntityManager will automatically propagate these changes to the database (if this happens within a commit statement). These objects are known as persistent object. 
  • If the Entity Manager is closed (via close()) then the managed entities are in a detached stateThese are known as the detached objects. If you want synchronize them again with the database, the a Entity Manager provides the merge() method. Once merged, the object(s) becomes perstent objects again.
  • The EntityManager is the API of the persistence context, and an EntityManager can be injected directly in to a DAO without requiring a JPA Template. The Spring Container is capable of acting as a JPA container and of injecting the EntityManager by honoring the @PersistenceContext (both as field-level and a method-level annotation).

Is it helpful? Add Comment View Comments
 

Most helpful rated by users:

©2024 WithoutBook