JPA Introduction

What is JPA ?

The process of mapping Java objects to database tables and vice versa is called "Object-relational mapping" (ORM).

Java Persistence API (JPA) provides Plain Old Java Object standard and object relational mapping (OR mapping) for data persistence among applications. JPA is a specification and several implementations are available. Popular implementations are Hibernate, EclipseLink and Apache OpenJPA. The reference implementation of JPA is EclipseLink.

By using JPA the developer can map, store, update and retrieve data from relational databases to Java objects and vice versa. JPA permits the developer to work directly with objects rather then with SQL statements.

The JPA implementation is typically called persistence provider. JPA can be used in Java-EE and Java-SE applications.

What is JPA ?

The process of mapping Java objects to database tables and vice versa is called "Object-relational mapping" (ORM).

Java Persistence API (JPA) provides Plain Old Java Object standard and object relational mapping (OR mapping) for data persistence among applications. JPA is a specification and several implementations are available. Popular implementations are Hibernate, EclipseLink and Apache OpenJPA. The reference implementation of JPA is EclipseLink.

By using JPA the developer can map, store, update and retrieve data from relational databases to Java objects and vice versa. JPA permits the developer to work directly with objects rather then with SQL statements.

The JPA implementation is typically called persistence provider. JPA can be used in Java-EE and Java-SE applications.




The JPA implementation is typically called persistence provider. JPA can be used in Java-EE and Java-SE applications.

The mapping between Java objects and database tables is defined via persistence metadata. JPA typically defines the metadata via annotations in the Java class. Alternatively the metadata can be defined via external defined XML or a combination of both XML and Annotation. A XML configuration overwrites the annotations.

JPA uses Annotations or XML to map Objects to a database,these objects are called Entities.

Entities are Plain Old Java Objects(POJO).

JPA has its own Query Language called Java Persistence Query Language(JPQL).It generates all the necessary SQL calls for CRUD operation.


Typical Web Application using JPA

ORM Overview


Application communicates with the configured JPA provider(Here Hibernate) to access the database.

The information about the mapping between the instance variables of classes and the columns of the tables in the database is mapped either through XML ans/or POJO with annotations.

POJO are Java Classes that represent the tables in the database.

The application talks to hibernate using JPA to perform CRUD operations on the database tables.


Architecture Of JPA:

JPA Architecture


Persistence:

The javax.persistence.Persistence class contains methods to obtain EntityManagerFactory instances.


EntityManagerFactory:

The EntityManagerFactory is used to create an instance of EntityManager.The EntityManagerFactory is created with the help of Persistence Unit during the application start up.It serves as a factory for EntityManager.


EntityManager:

The EntityManager interface is providing the API for interacting with the Entity.It provides the main interface to perform actual database operations.
All the POJO's i.e persistent objects are saved and retrieved with the help of an EntityManager Object.
Typically,EntityManager objects are created as needed and destroyed when not required.



Entity Bean:

Entity is the persistence (POJO) objects that represent one record in the table. The Entity is simple annoted POJO class, which is easy to develop.

Consider the following simple java class, named Employee.

class Employee{

 private int empId;
 private String name;
 private String phone;
 private String email;

// set/get
}

The above class represents a Employee object which has id, name, phone and email.If we want to persist this Employee java class using JPA we need to use @Entity annotation.

@Entity :

The first step is to qualify this java class as an entity. This can be done by marking the class with @Entity annotation.We have to use @Entity annotation to make this POJO class an entity.We need to update the Employee class as shown below.

@Entity
class Employee{
  private int empId;
  private String name;
  private String phone;
  private String email;

// set/get
}

@Id :

An Entity Bean MUST have an Id, which can be a single attribute or several attributes ( composite primary key). The Id of the Entity Bea MUST be serializable.A field that is marked with @Id annotation will be treated as a primary key for the table by the persistent engine.In our case, since no two employee in the company have same ID so empId is an ideal candidate to be marked with @Id annotation.

Now our updated code will looks like.

@Entity
class Employee{

 @Id
 private int empId;
 private String name;
 private String phone;
 private String email;

// set/get
}

Now we can persist Employee class Object using JPA. @Entity and @Id annotation is sufficient for Entity bean for persisting using JPA.Following is the default values for table and column names in this case.

Table Name – EMPLOYEE
Column Names – [EMPID, NAME, PHONE and EMAIL]

If you are providing only @Entity annotation then by default table name will be java class name and column name will be variables name. In the above code it will be EMPID, NAME, PHONE and EMAIL.We can customize these values based on our requirement.


Customizing the Entity Bean :

In JPA defaults that are created and provided by a persistence engine are sufficient. However in certain situations you need to customize these values. The entity class can be customized with a set of annotations available in the javax.persistence package.

Change the default table name :

By default the table name will be name of the class. We can change the default table name with the help of @Entity annotation.Consider the code given below.

@Entity(name = "EMP_DB")
	public class Employee{
	...
	}

Now, the table name is changed to EMP_DB

Customizing the Column default values(@Column Annotation):

The default name of the columns, their size, whether they can accept null values or not etc., can be customized using the @Column annotation.

Consider the code given below.

    @Column(name = "EMP_NAME", nullable = false, length = 100)
	private String name;

	@Column(name = "EMP_MAIL" nullable = true, length = 100)
	private String email;

The name property will override the default column name (which is the same as that of the field name in the Entity class). The nullable property tells that whether the column can accept null values. Length property is only applicable if the type of the column is String (or VARCHAR). There are also properties like scale and precision which is applicable only when the type of the column is NUMBER.


@GeneratedValue

This annotation provides the strategy to be used for the generation of the Id attributes of the Entity Bean. It is implementation specific and its behaviour is not defined on the specification. There are 4 possible choices :

  • auto
  • identity
  • table
  • sequence


EntityTransaction:

A Transaction represents a unit of work with the database.
Any kind of modifications via the EntityManager Object are placed within a transaction.An EntityManager object helps creating an EntityTransaction Object.


Query:

Persistent Objects are retrieved using a Query object.
Query objects allows using SQL or Java Persistence Query Language[JPQL] to retrieve the actual data from the database.





comments powered by Disqus