JPA Interview Questions and Answers
The Best LIVE Mock Interview - You should go through before Interview
Intermediate / 1 to 5 years experienced level questions & answers
Ques 1. 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).
Is it helpful?
Add Comment
View Comments
Ques 2. What is Embeddable classes?
- Entities may use persistent fields, persistent properties, or a combination of both.
- If the mapping annotations are applied to the entity's instance variables, the entity uses persistent fields.
- If the mapping annotations are applied to the entity's getter methods for JavaBeans-style properties, the entity uses persistent properties.
Is it helpful?
Add Comment
View Comments
Ques 3. What data types are allowed in the attributes of the Entity class (fields or properties)?
Valid attribute types for Entity classes are:
- primitive types and their Java wrappers,
- strings,
- any Java serializable types,
- enums;
- entity types;
- embeddable classes
- collection types
Is it helpful?
Add Comment
View Comments
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?