0% found this document useful (0 votes)
20 views

JAVA

Uploaded by

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

JAVA

Uploaded by

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

GROUP 2

Java API, JPA


architecture, ORM,CRUD
operations using JDBC.
Group 1 Abhinav Maitreya (23BAI10762)
2 Amlan Jyoti Sarmah (23BAI10565)
Members 3 Ayushman Rai (23bsa10023)
4 U.V SAI HEMANTH(23MIM10088)
5 K.PRUTHVI RAJ (23MIM10151)

6 ...
7 ...
CONTENTS
1. Introduction to Java Persistence API
2. JPA Architecture
3. JPA Installation
4. ORM Components
5. Creating JPA Entities
6. Advanced Mappings
7. Java Persistence Query Language (JPQL)
8. Performing CRUD Operations Using the JDBC API
Java Persistent
API (JPA)
The Java Persistence API (JPA) is a Java specification that
simplifies the management of relational data in
applications. It provides a framework for object-relational
mapping (ORM), enabling developers to interact with
databases using Java objects instead of complex SQL
queries. JPA is a standard interface implemented by
popular frameworks like Hibernate and EclipseLink,
making it easier to persist, retrieve, and manage data in
an object-oriented way. Its primary goal is to reduce
boilerplate code and improve productivity while ensuring
compatibility with multiple database systems.
Key Features
Simplicity: Automates the process of database interaction, reducing boilerplate code.
Object-Oriented Approach: Work with objects instead of raw data (tables and columns).
Platform Independence: Works with multiple databases and is not tied to a specific
provider.
Standardization: A standard interface implemented by popular ORM frameworks like
Hibernate and EclipseLink.
Ease of Use: Reduces complexity compared to JDBC (Java Database Connectivity).
Productivity: Developers focus on business logic instead of database management.
Maintainability: Promotes clean and modular code.
Portability: Easily switch between databases without changing the code.

For example,
Imagine an e-commerce application that needs to store information about products,
customers, and orders. Using JPA, you can represent each table (e.g., Products) as a Java
class, making database operations more intuitive and object-oriented.
The JPA architecture consists of several

JPA
components that work together to handle
database interactions:

Architecture
1. Entity Classes
2. Entity Manager
3. Persistence Unit
4. Persistence Context
Entity Classes
Represent database tables as
Java classes.
Each instance of an entity
corresponds to a row in the
table.

Entity Manager
The core interface for
interacting with the persistence
context.
Responsible for managing entity
lifecycle (e.g., persist, merge,
remove).
Persistence Unit
Defines a set of entity classes that are managed
together and configuration details (e.g., database
connection).
Configured in the persistence.xml file.

Persistence Context
A runtime environment where entities are managed.
Ensures that entities are synchronized with the
database.
How the Architecture Works:

1. The application interacts with the


Entity Manager.
2. The Entity Manager manages
entities and their states in the
Persistence Context.
3. Changes in the entities are
propagated to the database using
the JPA provider (e.g., Hibernate).
4. The Persistence Unit specifies
the configuration for the provider
and database connection.
JPA
INSTALLATION
Here is the process on how to install JPA in our
windows system.
INSTALLATION PROCESS
STEP 1: CREATE THE JAVA PROJECT
Create a Maven project and add JPA implementation
INSTALLATION PROCESS
STEP 2: DEPENDENCIES
We can dd the dependencies of the Persistence(JPA) of the application.
MySQL Dependency:
INSTALLATION PROCESS
STEP 3: FILE STRUCTURE
Once the create the JPA project then the file structure looks like the below image.
INSTALLATION PROCESS
STEP 4: CREATE THE EMPLOYEE ENTITY
INSTALLATION PROCESS
STEP 6: CONFIGURING PERSISTENCE.XML
INSTALLATION PROCESS
STEP 7: CREATE THE MAIN APPLICATION AND RUN IT
Once the create the JPA project then the file structure looks like the below image.
Introduction to ORM and JPA
ORM is a technique for converting data between
Java objects and relational databases (table). In
simple words, we can say that the ORM
implements responsibility of mapping the object
to relational model and vice-versa. the ORM tool
does mapping in such a way that model class
becomes a table in the database and each instance
becomes a row of the table.
Key Components of ORM
Entities: Java classes that represent
database tables. Each instance of the
entity class corresponds to a row in the
table.
Example: @Entity annotation
Session/EntityManager: Interfaces that
handle interactions between the
application and the database.
Session (in Hibernate) and EntityManager
(in JPA) manage the lifecycle of entities.
Supports CRUD operations (Create, Read,
Update, Delete).
EntityManager (JPA) or Session (Hibernate)
Manages the persistence context (i.e., the set of entities tracked by JPA).
Supports operations like:
find(): Retrieves an entity by its primary key.
persist(): Makes a new entity instance persistent.
merge(): Updates an entity.
remove(): Deletes an entity.
Query API in ORM
PQL (Java Persistence Query Language): Object-oriented query
language usewith JPA.
Similar to SQL but works with entities instead of database tables.
Supports SELECT, UPDATE, DELETE operations on entities.
Advantages
It saves time and efforts.
It gives pace to development process.
It reduces the development cost.
It provides connectivity to the database.
It makes development more object-oriented.
Easy transaction management.
No need to implement database manually.
Modification in any model (object or relational model) does not affect each other.
TITLE:
INTRODUCTION TO JPA ENTITIES
What are JPA Entities?
Java classes that represent database tables.

Part of Java Persistence API (JPA) for ORM (Object-


Relational Mapping).
2. Why Use JPA Entities?
3. Required Dependencies:
Simplifies database interaction.
Automates table-to-object mapping. For Maven:

For Gradle:
Creating a JPA Entity

Step 1: Annotate the Class


@Entity: Marks the class as a JPA entity.
@Table(name = "table_name"): Maps the entity to a specific database table.

Step 2: Configure Primary Key


@Id: Denotes the primary key field.
@GeneratedValue(strategy = GenerationType.IDENTITY): Specifies the primary key generation strategy.

Step 3: Map Fields to Columns


@Column(name = "column_name", nullable = false, unique = true): Maps fields to database
columns.
Example Code:
Explanation :
1. @Entity: Marks this class as a database table.
2. @Table(name = "users"): Names the table users.
3. @Id: Marks id as the primary key.
4. @GeneratedValue(strategy = GenerationType.IDENTITY): The database auto-generates the id
(like auto-increment).
5. @Column: Maps fields to table columns:
username: Must be unique and not empty.
password: Cannot be empty.
This code creates a users table with three columns: id, username, and password.
Advanced Features & Relationships

1. Common Annotations:
@PrePersist / @PreUpdate: Lifecycle hooks.
@Transient: Excludes a field from persistence.

2. Defining Relationships:
@OneToOne: One-to-one mapping.
@OneToMany & @ManyToOne: Parent-child relationships.
@ManyToMany: Many-to-many relationships.
Repository for Entity:
Interface for database operations:
Introduction to Java Persistent
Query Language (JPQL)
What is JPQL?

A query language for interacting with relational databases in


Java.
Used in the Java Persistence API (JPA) to query entities stored
in a database.

Key Features:

Object-oriented query syntax, not tied to database tables.


Supports SQL-like queries with entity objects instead of tables.
Platform-independent: Works with multiple databases.

Why JPQL?

Simplifies data access for Java developers.


Maintains a clear abstraction over the database layer.
Basic JPQL Syntax and Examples
Basic Syntax:

SELECT e FROM Entity e WHERE e.attribute = :value

SELECT: Retrieves data.


FROM: Specifies the entity class.
WHERE: Adds conditions to filter results.
Basic JPQL Syntax and Examples
CRUD OPERATIONS - JDBC
CRUD stands for Create, Read, Update and Delete Operations. To demonstrate
CRUD Operations using JBDC, We implement a TODO List operations.
CRUD OPERATION - JDBC
STEP 1: SETTING UP THE DATABASE
CRUD OPERATIONS - JDBC
STEP 2: SETTING UP THE CONNECTION
CRUD OPERATION - JDBC
STEP 3: CREATE OPERATION

STEP 4: READ OPERATION


CRUD OPERATION - JDBC
STEP 5: UPDATE OPERATION

STEP 6: DELETE OPERATION


CRUD
OPERATIONS
Using Java Persistence API
CRUD OPERATION - JPA
STEP 1: SETTING UP THE DATABASE
CRUD OPERATIONS - JPA
STEP 2: SETTING UP THE CONNECTION
CRUD OPERATIONS - JPA
CRUD OPERATION - JDBC
STEP 3: CREATE OPERATION

STEP 4: READ OPERATION


CRUD OPERATION - JDBC
STEP 5: UPDATE OPERATION
CRUD OPERATION - JDBC
STEP 6: DELETE OPERATION
THANK YOU!

You might also like