COMPONENTS OF JPA (OR)
CLASS LEVEL ARCHITECHTURE OF JPA
• The following image shows the class level architecture of JPA. It shows the
core classes and interfaces of JPA.
DATABASE
• Persistence : This class contains static methods , like
createEntityManagerFactory(String persistenceUnitName) to
obtain EntityManagerFactory instance. It accepts the persistence
unit name as input and loads all the properties from the
persistence file.
• EntityManagerFactory : This is a factory class of EntityManager.
It consists of createEntityManager() method creates , returns and
manages multiple EntityManager instances.
• EntityManager : is the primary JPA interface. The EntityManager
manages the entities and consists of methods to perform
database operations such insert , update , delete and retrieve
objects from database.
• It consists of following methods :
• persist(object) : It is used to store(insert) an entity object into the database.
• remove(object) : It is used to remove an entity object from the database.
• merge(object) : It is used to update the existing object in the database by merging
the states of given entity object.
• find(entityClassName.class , primaryKeyValue) : Used to find / search for object
based on primary key.
• getTransaction() : creates and return an object of EntityTransaction.
• createQuery(String sqlString) : It accepts a Java Persistence query language
(JPQL) query as an input and creates an object of Query for executing JPQL
statement.
• EntityTransaction : For each EntityManager, operations are maintained by
EntityTransaction. It is used to keep track of transactions performed by the
EntityManager. It consists of methods such as :
• begin() : it is used to specify that transaction has started.
• commit() : it specifies that the transaction has ended and saves then
changes to the database.
• Query : The object of Query can be obtained by calling the createQuery()
of EntityManager interface. The Query object is used to execute the JPQL /
HQL/SQL queries. For executing the queries , it consists of methods such as ,
• int executeUpdate() : is used to execute the update or delete query.
• List<E> getResultList() : is used to execute the select query. It returns
the results as a List.
ANNOTATIONS
@Entity : Used for declaring any POJO class as an entity(table) for a
database. It maps the class with the table in the database.
@Table : Used to change table details, such as name of the table. If you don't
use @Table annotation, hibernate will use the class name as the table name
by default.
@Id : marks the identifier for the entity. It is Used for declaring a primary
key inside our entity class.
@GeneratedValue : Used to automatically generate the primary key values
based on certain strategies and we don’t need to set the values manually.
• @Column : used to specify the details of the column to which a field or
property will be mapped. @Column annotation can be used with following
attributes :
• name attribute allows to specify the name of the column
explicitly.
• length attribute allows to specify the size of the column ,
particularly for a String value.
• nullable attribute allows to specify whether null values are
allowed in the column or not.
• unique attribute allows to specify whether duplicate values are
allowed or only unique values are allowed in the column.
• Test class for select HQL query.
• Test class for select HQL query based on condition.
• Test class for delete HQL query.
• Test class for update HQL query.
• Test class for update HQL query by taking input from user.
• Annotations examples : EXAMPLE 1
EXAMPLE 2