Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
27 views

Java Persistence API

Uploaded by

kavishna
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

Java Persistence API

Uploaded by

kavishna
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 33

Java Persistence API

1. Introduction To Java Persistence API:


2. ORM
3. Database And The Application-
4. Architecture Of JPA
5. How JPA Works?-JPA Specifications.
Introduction to Java Persistence API
(JPA)
• Java Persistence API is a collection of classes and
methods to persistently store the vast amounts of
data into a database which is provided by the Oracle
Corporation.
• To reduce the burden of writing codes for relational
object management, a programmer follows the ‘JPA
Provider’ framework, which allows easy interaction
with database instance.
Introduction to Java Persistence API
(JPA)
• The Java Persistence API (JPA) is a specification of Java.
• It is used to persist data between Java object and relational
database.
• JPA acts as a bridge between object-oriented domain models
and relational database systems.
• As JPA is just a specification, it doesn't perform any
operation by itself. It requires an implementation.
• So, ORM tools like Hibernate, TopLink and iBatis
implements JPA specifications for data persistence.
Introduction to Java Persistence API
(JPA)
JPA History:
• Earlier versions of EJB, defined persistence layer
combined with business logic layer using
javax.ejb.EntityBean Interface.
• While introducing EJB 3.0, the persistence layer was
separated and specified as JPA 1.0 (Java Persistence API).
• The specifications of this API were released along with
the specifications of JAVA EE5 on May 11, 2006 using
JSR 220.
Introduction to Java Persistence API
(JPA)
JPA History:
• JPA 2.0 was released with the specifications of JAVA EE6
on December 10, 2009 as a part of Java Community
Process JSR 317.
• Following are the important features of this version: -
• It supports validation.
• It expands the functionality of object-relational
mapping.
• It shares the object of cache support.
Introduction to Java Persistence API
(JPA)
JPA History:
• JPA 2.1 was released with the specification of JAVA EE7
on April 22, 2013 using JSR 338 with the following
features: -
• It allows fetching of objects.
• It provides support for criteria update/delete.
• It generates schema.
Introduction to Java Persistence API
(JPA)
JPA History:
• JPA 2.2 was released with the specification of JAVA EE8
as a development of maintainenece in 2017.
• Some of its important feature are: -
• It supports Java 8 Date and Time.
• It provides @Repeatable annotation that can be used when user
want to apply the same annotations to a declaration or type use.
• It allows JPA annotation to be used in meta-annotations.
• It provides an ability to stream a query result.
Introduction to Java Persistence API
(JPA)
JPA Providers:
• JPA is an open source API, therefore various enterprise
vendors such as Oracle, Redhat, Eclipse, etc. provide new
products by adding the JPA persistence flavor in them.
• Some of these products include: Hibernate, Eclipselink,
Toplink, Spring Data JPA, etc.
JPA ORM
• Object Relational Mapping (ORM) is a functionality which is
used to develop and maintain a relationship between an object
and relational database by mapping an object state to database
column.
• It is capable to handle various database operations easily such
as inserting, updating, deleting etc.
JPA ORM

ORM Frameworks
• Following are the various frameworks that function on ORM
mechanism: -
• Hibernate
• TopLink
• ORMLite
• iBATIS
• JPOX
JPA ORM

Mapping Directions
• Mapping Directions are divided into two parts: -
• Unidirectional relationship - In this relationship, only one
entity can refer the properties to another. It contains only one
owing side that specifies how an update can be made in the
database.
• Bidirectional relationship - This relationship contains an
owning side as well as an inverse side. So here every entity
has a relationship field or refer the property to other entity.
JPA ORM

Types of Mapping:
Following are the various ORM mappings: -
• One-to-one - This association is represented by @OneToOne
annotation. Here, instance of each entity is related to a single
instance of another entity.
• One-to-many - This association is represented by
@OneToMany annotation. In this relationship, an instance of
one entity can be related to more than one instance of another
entity.
JPA ORM

Types of Mapping:
Following are the various ORM mappings: -
• Many-to-one - This mapping is defined by @ManyToOne
annotation. In this relationship, multiple instances of an
entity can be related to single instance of another entity.
• Many-to-many - This association is represented by
@ManyToMany annotation. Here, multiple instances of an
entity can be related to multiple instances of another entity.
In this mapping, any side can be the owing side.
Architecture Of JPA

• Java Persistence API is a source to store business entities as


relational entities.
• It shows how to define a PLAIN OLD JAVA OBJECT
(POJO) as an entity and how to manage entities with
relations.
Architecture Of JPA
Class Level Architecture:
• The following image shows the class level architecture of JPA.
It shows the core classes and interfaces of JPA.
Architecture Of JPA

• The following table describes each of the units shown in the


above architecture.
S.no Units Description
This is a factory class of EntityManager.
1 EntityManagerFactory It creates and manages multiple
EntityManager instances.
It is an Interface, it manages the
2 EntityManager persistence operations on objects. It
works like factory for Query instance.

Entities are the persistence objects, stores


3 Entity
as records in the database.
Architecture Of JPA
S.no Units Description
It has one-to-one relationship with
EntityManager. For each EntityManager,
4 EntityTransaction
operations are maintained by
EntityTransaction class.

This class contain static methods to obtain


5 Persistence
EntityManagerFactory instance.

This interface is implemented by each JPA


6 Query vendor to obtain relational objects that meet
the criteria.
• The above classes and interfaces are used for storing entities
into a database as a record.
Architecture Of JPA
JPA Class Relationships:
• In the above architecture, the relations between the classes and interfaces
belong to the javax.persistence package.
• The following diagram shows the relationship between them.
Architecture Of JPA
JPA Class Relationships:
• The relationship between EntityManagerFactory and EntityManager is
one-to-many. It is a factory class to EntityManager instances.
• The relationship between EntityManager and EntityTransaction is
one-to-one. For each EntityManager operation, there is an
EntityTransaction instance.
• The relationship between EntityManager and Query is one-to-many.
Many number of queries can execute using one EntityManager
instance.
• The relationship between EntityManager and Entity is one-to-many.
One EntityManager instance can manage multiple Entities.
Database and the Application

Database in JSP:
• JPA provides a convenient way to interact with databases without
having to write low-level SQL code.
ORM:
• ORM stands for Object-Relational Mapping, which is a
programming technique that allows software engineers to write in
any programming language of their choice and encapsulate (hide)
the code needed to manipulate the data, so user don't use SQL
anymore;
• User interact directly with an object in the same language you're
writing.
Database and the Application

ORM:
• The goal of ORM is to help software engineers work with databases
by allowing them to interact with the database using object-oriented
programming concepts, such as classes, objects, and methods, rather
than having to write low-level SQL code.
• For example, working with database using Java application.
• User have a database, and use JDBC to connect the Java application to the
database.
• Where user have a class and the class structure, that class has some variables,
and then have a table.
• Next will implement, table name will be the class name, and the columns will
be the class variable. And, this is how we use object-relational mapping.
Database and the Application

• To implement ORM in Java, need some tools.


• These tools include Hibernate, Entity Framework, Sequelize,
SQLAlchemy, and Dapper.
• User can build an ORM application using Hibernate.
• To save objects in a database with rows and columns, user have to
use a mechanism that maps the objects to the relational database
model.
• This is where the Java Persistence API (JPA) comes in.
• It is an object-relational mapping (ORM) framework that allows us
to map Java objects to tables in a relational database.
Database and the Application
• To implement ORM in Java, need some tools.
• These tools include Hibernate, Entity Framework, Sequelize,
SQLAlchemy, and Dapper. User can build an ORM application using
Hibernate.
• This is where the Java Persistence API (JPA) comes in.
• It is an object-relational mapping (ORM) framework that allows us to
map Java objects to tables in a relational database.
• Hibernate is an implementation of the JPA specification and
provides a set of APIs for interacting with databases using objects.
• To save objects in a database with rows and columns, user have to use a
mechanism that maps the objects to the relational database model.
Database and the Application
• To implement Hibernate with JPA, need to include the Hibernate
dependencies in your project.
• Also need to configure Hibernate using a persistence.xml file, which
should be placed in the META-INF folder of your project.
Create entity management class:
EntityManagerFactory entityManagerFactory =
Persistence.createEntityManagerFactory("my-persistence-unit");
• From the code above, can create an EntityManagerFactory instance
based on the configuration in the persistence.xml file.
Database and the Application
Create entity management instance:
EntityManager entityManager =
entityManagerFactory.createEntityManager();
• This code creates an EntityManager instance, which you can use to
perform database operations such as fetching and saving entities.
Fetch an entity from database:
• To fetch an entity from the database, use the "find" method of the
EntityManager class, as shown below:
Customer customer = entityManager.find(Customer.class, 1L);
• The code above fetches the customer with ID 1 from the database and
maps it to a "Customer" object.
Database and the Application
Save an entity to the database:
• To save an entity to the database, use the "persist" method of the
EntityManager class, as shown below:
entityManager.getTransaction().begin();
Customer customer = new Customer();
customer.setFirstName("Isah");
customer.setLastName("Jacob");
entityManager.persist(customer);

entityManager.getTransaction().commit();

• The code above creates a new "Customer" object, sets its first name and
last name, and then persists it to the database.
• The transaction is committed at the end, which ensures that the changes
are saved to the database.
Database and the Application
• In JPA, use annotations such as "@Entity" and "@Id" to map Java
objects to database tables.
• an example of a "Customer" entity class:
@Entity @Table(name = "customers")
public class Customer
{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "first_name")
private String firstName;
@Column(name = "last_name")
private String lastName;
//can add getters and setters here
}
Database and the Application
• The code above defines a "Customer" entity class with three fields: "id"
"firstName" and "lastName."
• The "@Entity" annotation specifies that this class should be mapped to
a database table.
• The "@Id" annotation specifies that the "id" field is the primary key for
this table, and the "@GeneratedValue" annotation specifies that this
field should be automatically generated by the database.
Retrieve a "Customer" object by its ID:
Customer customer = entityManager.find(Customer.class,
1L);
• This code retrieves the "Customer" object with ID 1 from the database.
Database and the Application

Applications in JPA:
• JPA combines the best features from previous persistence
mechanisms such as Java Database Connectivity (JDBC) APIs,
Object Relational Mapping (ORM) frameworks, and Java Data
Objects (JDO).
• Creating entities under JPA is as simple as creating serializable
classes.
• JPA supports the large data sets, data consistency, concurrent use,
and query capabilities of JDBC.
Database and the Application

• Like object-relational software and object databases, JPA allows the


use of advanced object-oriented concepts such as inheritance.
• JPA avoids vendor lock-in because it does not rely on a strict
specification like JDO and EJB 2.x entities.
• The JPA implementation does not mandate that user migrate existing
applications.
• Existing EJB 2.x Container Manager Persistence applications
continue to run without changes
• With the JPA tools in the product, can use wizards to create and
automatically initialize mappings.
Database and the Application

• User can create new database tables from existing entity classes
(top-down mapping) or new entity beans from existing database
tables (bottom-up mapping).
• User can also use the tools to create mappings between existing
database tables and entity beans (meet-in-the-middle mapping),
where names or other attributes differ.
• Also create mappings from several types of Java class, and can
specify entity inheritance with several options for database design.
Specification Of JPA

• The Java Persistence API (JPA) specification defines how to map


objects to relational databases.
• JPA uses XML or annotations to map objects to one or more
database tables.
• JPA specifications can also be used to dynamically build queries for
database operations.
• This can be done by using the Specification interface in Spring Data
JPA.
Specification Of JPA

• The Specification interface allows developers to:


• Create complex queries at runtime.
• Define criteria to specify query conditions.
• Combine criteria using logical operators like AND and OR.
• Avoid writing multiple query methods for different search criteria.
• JPA specifications can be used:
• Create Predicate Queries: Predicate queries can be reused and
combined with other predicates.
• Compose queries from multiple filtering options: Queries can be
built dynamically based on user input.
• Customize the Criteria API query: Developers can customize the
entire Criteria API query that will be executed by Spring Data JPA.

You might also like