JPA Interview Questions and Answers
Related differences
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.
Ques 2. What is JPA and its features?
- 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.
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.
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.
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 state. These 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).
Most helpful rated by users:
- What is ORM in JPA?
- What is an EntityManager?
- What is the difference between persistence.xml and hibernate.cfg.xml?
- What is an Entity?
- Why to use JPA?