HSF301_Chapter_01
HSF301_Chapter_01
Objectives
Explain what JPA is and its role in data persistence for Java applications.
Highlight the advantages of using JPA over traditional JDBC approaches.
Define and illustrate core JPA concepts like entities, annotations,
persistence context, entity manager and JPQL.
Provide a clear understanding of how these components work together to
manage data.
Show how JPA simplifies development by reducing boilerplate code and allo
wing focus on business logic.
2
Contents
Introduction
Key Concepts
Annotations
Relationships
Using JPA
Demo
Advantages and Disadvantages
3
What is JPA ?
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.
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.
4
Key Feature of JPA
ORM: JPA allows you to map Java objects (entities) to relational database
tables, abstracting away the differences between the object-oriented and
relational models.
Annotations: You can use annotations to define entities, relationships and
other persistence related metadata.
EntityManager: The interface provides methods for persisting, retrieving,
updating, and deleting entities.
JPQL is an object oriented query language that allows you to query entities
and their relationships.
Transaction Management: JPA supports transaction management, ensuring
data integrity and consistency.
5
Benefits of Using JPA
Simplified Data Access: JPA makes it easier to work with relational data in Ja
va applications by providing a higher-level abstraction.
Code Portability: JPA is a standard specification, so code written using
JPA can be portable across different JPA providers.
Reduced Boilerplate Code: JPA annotations and the EntityManager
interface reduce the amount of code needed for data access.
Improved Data Integrity: JPA's transaction management and validation
features help ensure data integrity.
Enhanced Performance: Caching and other optimization techniques can
improve the performance of JPA applications.
6
JPA Implementation Options
7
Annotations in JPA
Entity Annotations in JPA
@Entity: This annotation marks a Java class as an entity, meaning it
represents a table in the database.
@Table: This annotation allows you to specify the name of the database table
that an entity maps to.
@Transient: This annotation marks a property that should not be persisted to
the database.
@NamedQueries and @NamedQuery: These annotations allow you to define
named queries that can be used to retrieve entities.
9
Mapping Annotations in JPA
@Id: This annotation marks a Java class as an entity, meaning it represents a table i
n the database.
@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.
@Basic: This annotation specifies that a property is a basic type and should be
persisted.
@Enumerated: This annotation is used to map an Enum type to a database column.
10
Relationships Annotations in JPA
@ManyToOne: This annotation defines a many-to-one relationship between
two entities.
@OneToMany: This annotation defines a one-to-many relationship between
two entities.
@OneToOne: This annotation defines a one-to-one relationship between two
entities.
@ManyToMany: This annotation defines a many-to-many relationship between
two entities.
11
Demo JPA
(One To Many)
1. Open Eclipse, File | New | Maven Project
13
2. Check Create a simple project -> Browse Project -> Next
14
3. Fill the information Project -> Click Finish
15
4. Structure of Maven Project
16
4. Structure of Maven Project
17
5. Change the content of pom.xml
18
6. Create persistence.xml in META-INF folder
19
7. Create structure of project
20
8. Config JPA
21
9. Create Books in Pojo
22
10. Create Students in Pojo
23
11. Create StudentDAO
24
12. Create IStudentRepository
25
13. Create StudentRepository
26
14. Create IStudentService
27
15. Create StudentService
28
16. Create Main function
29
18. Run Program
30
19. Result
31
20. Result
32
Demo JPA
(Many To Many)
Q&A
34