Chapter 01 - JPA, JPA Mapping
Chapter 01 - JPA, JPA Mapping
Objectives
v Explain what JPA is and its role in data persistence for Java applications.
v Highlight the advantages of using JPA over traditional JDBC approaches.
v Define and illustrate core JPA concepts like entities, annotations,
persistence context, entity manager and JPQL.
v Provide a clear understanding of how these components work together to
manage data.
v Show how JPA simplifies development by reducing boilerplate code and allo
wing focus on business logic.
2
Contents
v Introduction
v Key Concepts
v Annotations
v Relationships
v Using JPA
v Demo
v Advantages and Disadvantages
3
Object Relational Mapping
(ORM)
JPA Object Relational Mapping
v ORM makes it easier to work with databases in object-oriented applications,
allowing you to focus on the business logic rather than the underlying data
storage details.
5
Benefits of ORM
◆ Increased Productivity: You write less code because ORM handles the
low-level data access tasks.
◆ Improved Maintainability: Your code is cleaner and easier to understand
because it focuses on the business logic, not database details.
◆ Enhanced Performance: ORM frameworks can optimize data access and
caching, potentially improving performance.
◆ Database Independence: You can switch databases without rewriting your a
pplication code (to some extent)
6
Popular ORM Frameworks for Java
v Hibernate: The most widely used JPA implementation, offering a rich feature
set and extensive customization options.
v EclipseLink: Another JPA implementation with a focus on standards complia
nce and portability.
v Spring Data JPA: An abstraction layer on top of JPA providers, simplifying d
ata access and reducing boilerplate code.
v DataNucleus providing easy persistence to RDBMS datastores. Comes with
its own "SQL-like" JPQL query language, so you query your data in a
language similar to what your datastore understands.
7
Java Persistence API
(JPA)
JPA Versions and Key Features
JPA Version Release Year Key Features
Core ORM functionality: entities, mappings, relationships, i
JPA 1.0 2006
nheritance, JPQL, EntityManager
Enums, Criteria API, embeddable collections, derived prop
JPA 2.0 2009
erties, validation, metamodel
Entity graphs, converters, stored procedures, Java 8 date/t
JPA 2.1 2013
ime
JPA 2.2 2019 Streamlined bootstrap, Java 9 modules
Renamed to Jakarta Persistence, Java records, Java 17 s
JPA 3.0 2022
upport
9
JPA Implementation Options
10
What is JPA ?
v JPA stands for Java Persistence API. It is a Java programming interface that
allows developers to manage relational data in Java applications using object
oriented methodologies.
v JPA is a specification for managing relational data in Java applications. It
provides a set of interfaces, annotations, and an object relational mapping
(ORM) framework that simplifies the process of interacting with databases.
11
Key Feature of JPA
v ORM: JPA allows you to map Java objects (entities) to relational database
tables, abstracting away the differences between the object-oriented and
relational models.
v Annotations: You can use annotations to define entities, relationships and
other persistence related metadata.
v EntityManager: The interface provides methods for persisting, retrieving,
updating, and deleting entities.
v JPQL is an object oriented query language that allows you to query entities
and their relationships.
v Transaction Management: JPA supports transaction management, ensuring
data integrity and consistency.
12
Benefits of Using JPA
v Simplified Data Access: JPA makes it easier to work with relational data in Ja
va applications by providing a higher-level abstraction.
v Code Portability: JPA is a standard specification, so code written using
JPA can be portable across different JPA providers.
v Reduced Boilerplate Code: JPA annotations and the EntityManager
interface reduce the amount of code needed for data access.
v Improved Data Integrity: JPA's transaction management and validation
features help ensure data integrity.
v Enhanced Performance: Caching and other optimization techniques can
improve the performance of JPA applications.
13
Entity Annotations in JPA
v @Entity: This annotation marks a Java class as an entity, meaning it
represents a table in the database.
v @Table: This annotation allows you to specify the name of the database table
that an entity maps to.
v @Transient: This annotation marks a property that should not be persisted to
the database.
v @NamedQueries and @NamedQuery: These annotations allow you to define
named queries that can be used to retrieve entities.
14
Mapping Annotations in JPA
v @Id: This annotation marks a Java class as an entity, meaning it represents a table in
the database.
v @Column: This annotation defines the mapping between a property of an entity and
a column in the database table. You can use it to specify the column name, data type,
and other attributes.
v @Basic: This annotation specifies that a property is a basic type and should be
persisted.
v @Enumerated: This annotation is used to map an Enum type to a database column.
15
JPA Entity Manager
v The entity manager implements the API and encapsulates all of them within a
single interface.
16
Key Operations of the Entity Manager
v Persisting Entities: em.persist(entity) makes a transient entity instance persistent
v Finding Entities: em.find(entityClass,primaryKey) retrieves an entity by its primary
key.
v Merging Entities: em.merge(entity) merges a detached entity instance into the
current persistence context.
v Removing Entities: em.remove(entity) removes a persistent entity instance.
v Querying Data: em.createQuery(jpqlString) creates a JPQL query to retrieve entities
based on specified criteria.
17
Demo Simple CRUD with JPA
1. Open Eclipse, File | New | Maven Project
19
2. Check Create a simple project -> Browse Project -> Next
20
3. The New Project
21
4. Create folder META-INF in src/main/resources
22
5. Edit the pom.xml
23
6. Create persistence.xml in META-INF folder
24
7. Create persistence.xml in META-INF folder
25
8. Create persistence.xml in META-INF folder
26
9. Edit the persistence.xml in META-INF folder
27
10. Add com.fe.pojo Package in src/main/java
28
11. Add Student.java in com.fe.pojo package
29
12. Edit the Student.java
30
13. Add com.fe.dao Package in src/main/java
31
14. Create StudentDAO in com.fe.dao
32
15. Edit the StudentDAO in com.fe.dao
33
16. Save Student Method
34
17. Get All Students Method
35
18. Find a Student Method
36
19. Delete Student Method
37
20. Update Student Method
38
21. Add com.fe.main Package in src/main/java
39
22. Create StudentMain class in com.fe.main Package
40
23. Edit the StudentMain in com.fe.main
41
24. Edit the StudentMain in com.fe.main
42
25. Result
43
Mapping in JPA
Relationships Annotations in JPA
v @ManyToOne: This annotation defines a many-to-one relationship between
two entities.
v @OneToMany: This annotation defines a one-to-many relationship between
two entities.
v @OneToOne: This annotation defines a one-to-one relationship between two
entities.
v @ManyToMany: This annotation defines a many-to-many relationship between
two entities.
45
Demo JPA
(One To Many)
1. Open Eclipse, File | New | Maven Project
47
2. Check Create a simple project -> Browse Project -> Next
48
3. Fill the information Project -> Click Finish
49
4. Structure of Maven Project
50
5. Create persistence.xml in META-INF folder
51
6. Create structure of project
52
7. Create Books in Pojo
53
8. Create Students in Pojo
54
9. Create StudentDAO
55
10. Save Student in StudentDAO
56
11. Get All Students in StudentDAO
57
12. Delete Student in StudentDAO
58
13. Get a Student in StudentDAO
59
14. Get a Student in StudentDAO
60
15. Create IStudentRepository
61
16. Create StudentRepository
62
17. Create IStudentService
63
18. Create StudentService
64
19. Create Main function
65
20. Run Program
66
21. Result
67
22. Result
68
Demo JPA
(Many To Many)
1. Create Books in Pojo’s Package
70
2. Create Students in Pojo’s Package
71
3. Run Program
72
4. Result
73
5. Result
74
Summary
v Concepts were introduced:
v Overview about JPA
v Explain and demo using Eclipse IDE to create JPA Console App
75