Interview Questions on Java EE – Java Persistence API (JPA) – Part 3

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:

1) 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),

2) Get data: find (find and get Entity), createQuery, createNamedQuery, createNativeQuery, contains, createNamedStoredProcedureQuery, createStoredProcedureQuery

3) Get other JPA entities: getTransaction, getEntityManagerFactory, getCriteriaBuilder, getMetamodel, getDelegate

4) Working with EntityGraph: createEntityGraph, getEntityGraph

4) General operations on EntityManager or all Entities: close, isOpen, getProperties, setProperty, clear.

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

1) If the status is 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.

How does the remove operation affect 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.

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

1) 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,

2) If the status new, removed or detached, will be thrown exception.

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.

Close

About The Author

Ruchi Singla
An enthusiast writer with years of experience, who loves to create beautiful thoughts into words. She writes with a zeal and enjoys reading vogue with a good cup of coffee when not writing. Ruchi is working as a content writer for many businesses, helping them gain an extra edge over their competitors. As a content writer, she is responsible for enhancing engagement and traffic on the blog by brainstorming exciting content ideas.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.