jpa architecture

The following two tabs change content below.
Prasad Kharkar is a java enthusiast and always keen to explore and learn java technologies. He is SCJP,OCPWCD, OCEJPAD and aspires to be java architect.

Latest posts by Prasad Kharkar (see all)

Hello all, In this section we will learn about the jpa architecture and major components of jpa framework.

JPA Architecture:

JPA architecture consists of EntityManager and EntityManagerFactory and Entity. We have seen entities in JPA in previous article .

EntityManagerFactory:

  • Responsible for creating EntityManager instances.
  • Initialized with the help of persistence context.

This is what javadoc says about EntityManagerFactory.

The EntityManagerFactory interface is used by the application to obtain an application-managed entity manager. When the application has finished using the entity manager factory, and/or at application shutdown, the application should close the entity manager factory. Once an EntityManagerFactory has been closed, all its entity managers are considered to be in the closed state.

EntityManagerFactory can be creating using following code:

Here, JPAExamples is the name of persistence unit.

Another important component in JPA architecture is the EntityManager which is responsible for entity operations.

EntityManager:

This component in JPA architecture manages entities.

  • Performs the real work of persistence.
  • Performs create, update and delete operations on entities in persistence context.
  • Brings entities into persistence context and then entity is supposed to be managed by EntityManager .
  • Multiple EntityManager instances can be created from EntityManagerFactory .

This is what javadoc says about EntityManager.

An EntityManager instance is associated with a persistence context. A persistence context is a set of entity instances in which for any persistent entity identity there is a unique entity instance. Within the persistence context, the entity instances and their lifecycle are managed. The EntityManager API is used to create and remove persistent entity instances, to find entities by their primary key, and to query over entities.

You can create EntityManager using

JPA architecture can be represented in the diagram below.

JPAArchitecture

  • There is one to one correspondence between persistence unit and EntityManagerFactory .
  • Multiple EntityManager instance refer to a single persistence context.
  • Multiple EntityManager instances can be created using single EntityManagerFactory

I hope this article helps understand JPA architecture.

Please feel free to suggest any improvements and provide feedback.

 

References for further study :

 

Share Button

Prasad Kharkar

Prasad Kharkar is a java enthusiast and always keen to explore and learn java technologies. He is SCJP,OCPWCD, OCEJPAD and aspires to be java architect.

4 thoughts on “jpa architecture

  • Pingback:JPA CRUD example | theJavaGeek

  • October 19, 2015 at 12:28 pm
    Permalink

    What does the word persistence ,persistence context and persistence unit means??
    Plz clear this with some example.

    Reply
  • December 13, 2016 at 3:01 am
    Permalink

    Hi.
    Please correct, it is an Entity Manager managing Persistence Context, not the opposite.

    Reply
    • December 13, 2016 at 1:27 pm
      Permalink

      I believe I have written same thing in passive voice 🙂 Please see second bullet point.

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *