JPA Architecture

Entity:

A class which have to be stored in the database tables is known as an entity. They should follow some simple rules of Plain Old Java Object programming model (POJO). An entity must be annotated with javax.persistence.Entity. By default, the table name corresponds to the class name. We can provide user-defined table name by using the annotation @Table(name=”TABLENAME”). By default, all fields of an Entity will persist. We have to use @Transient annotation for those fields which should not be saved. By default, each field is mapped to a column with the field name. We can provide user-defined column name by using the annotation @Column (name=”COLUMNNAME”).

Some rules that should be, not must be followed by an entity class. 1. An entity class should have a default constructor. 2. An entity class should have an id to uniquely identify the class objects.

Persistence:

Persistence class contain static methods to obtain EntityManagerFactory object. E.g. EntityManagerFactory entityManagerFactory = createEntityManagerFactory(“PersistenceUnit”);

EntityManagerFactory:

EntityManagerFactory is an interface which is responsible for creating and managing the EntityManager instances. It uses persistence context for initialization process.

Syntax:

EntityManagerFactory entityManagerFactory = createEntityManagerFactory("PersistenceUnit");

EntityManager:

EntityManager is an interface which is used to perform the persistence operations like create, update, delete etc. on objects. EntityManager works like factory for Query objects.

Syntax:

EntityManagerFactory entityManagerFactory = createEntityManagerFactory("PersistenceUnit");
EntityManager entityManager = entityManagerFactory.createEntityManager();

According to Javadoc: 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.

EntityTransaction:

EntityTransaction is a class which is used to manage the operations for an EntityManager instance. There is one-to-one relationship exists between EntityTransaction and EntityManager.

Query:

Query interface is the mechanism for issuing queries in JPA.

EntityManagerFactory entityManagerFactory = createEntityManagerFactory("PersistenceUnit");
EntityManager entityManager = entityManagerFactory.createEntityManager();
Query query = entityManager.createQuery("queryString");
Please follow and like us:
Content Protection by DMCA.com