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

Spring Data JPA

Spring Data JPA simplifies working with relational databases in Java by reducing boilerplate code and enhancing JPA with features like auto CRUD method generation and a cleaner repository architecture. It allows developers to use annotations for defining entities and relationships, and supports various types of object-relational mappings such as one-to-one, one-to-many, and many-to-many. The document also outlines the advantages of JPA over JDBC, including simplified data manipulation and the ability to represent data as objects.

Uploaded by

techjam017
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Spring Data JPA

Spring Data JPA simplifies working with relational databases in Java by reducing boilerplate code and enhancing JPA with features like auto CRUD method generation and a cleaner repository architecture. It allows developers to use annotations for defining entities and relationships, and supports various types of object-relational mappings such as one-to-one, one-to-many, and many-to-many. The document also outlines the advantages of JPA over JDBC, including simplified data manipulation and the ability to represent data as objects.

Uploaded by

techjam017
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Spring Data JPA

Presented by : JAMES KM
Software Developer at TechSwing Solutions pvt.ltd
What is Spring Data JPA

 Spring Data JPA is a part of the Spring Data project that makes it easier to work with
relational databases using Java Persistence API (JPA). It simplifies data access by
reducing the need for boilerplate code such as DAOs and custom SQL queries.
It uses JPA as the base technology, usually backed by Hibernate, and enhances it with:
• Spring-style configuration
• Auto CRUD method generation
• Cleaner repository-based architecture
Example Without Writing SQL:

public interface UserRepository extends JpaRepository<User, Long> {

List<User> findByLastName(String lastName); // No SQL needed

Spring Data JPA will auto-generate the SQL like:

SELECT * FROM users WHERE last_name = ?


Why Should We Use JPA?

JPA (Java Persistence API) is simpler, cleaner, and less labor-intensive than JDBC, SQL,
and hand-written mapping. JPA is suitable for non-performance-oriented complex
applications. The main advantage of JPA over JDBC is that, in JPA, data is represented by
objects and classes while in JDBC data is represented by tables and records. It uses POJO
to represent persistent data that simplifies database programming.
Advantages of JPA:

• Avoid Writing DDL: JPA avoids writing DDL in a database-specific dialect of SQL.
Instead, it allows mapping in XML or using Java annotations.
• Simplified DML: JPA allows us to avoid writing DML in the database-specific dialect
of SQL.
• Object Graphs: JPA allows us to save and load Java objects and graphs without any
DML language at all.
• JPQL: When we need to perform queries, JPQL allows us to express the queries in
terms of Java entities rather than the (native) SQL table and columns.
Object-Relation Mapping (ORM)
JPA Architecture
•Persistence: Contains static methods to obtain an EntityManagerFactory instance.
•EntityManagerFactory: Factory class for EntityManager. Creates and manages multiple
instances of EntityManager.
•EntityManager: Interface controlling persistence operations on objects, working with
the Query instance.
•Entity: Persistence objects stored as records in the database.
•Persistence Unit: Defines a set of all entity classes. Managed by EntityManager instances.
•EntityTransaction: Manages operations of EntityManager.
•Query: Interface for obtaining relational objects meeting the criteria.
Application Properties
Configure your database connection in `application.properties`:
Defining Entities

Annotations

•@Entity:Specifies that the class is


an entity.
•@Id: Denotes the primary key.
•@GeneratedValue: Specifies the

generation strategy for the primary


key.
Creating Repositories

Common Repository Methods

•save(S entity): Saves an entity.

•findById(ID id): Finds an entity by its


ID.

•findAll(): Returns all entities.

•deleteById(ID id): Deletes an entity by


its ID.
CRUD Operations Query Methods
Custom Queries
JPARelationShips
 One to one
 One to Many
 Many to one
 Many to Many

How to Achieve this in Spring data JPA in Sence ?

Java coders With the help of annotations relating to relation ships.


@OneToOne,@OneToMany,@ManyToOne,ManyToMany.
Instructor

Course

Course Material

Student

 Here we see one coure have one course material is (un-idirection) at the same one coure
material have a one course(bi-direction) mappings.

 One instructor may teach multiple course at the same time multiple course is taught by one
instructor.

 Many course is taken by one student at the same time many student taken by one course
1. One-to-One — Course ↔ @Entity
CourseMaterial public class CourseMaterial {
@Id @GeneratedValue //strategy may AUTO
@Entity or IDENTITY
public class Course { private Long id;
@Id @GeneratedValue private String contentUrl;
//strategy may AUTO or IDENTITY
private Long id; @OneToOne
private String title; @JoinColumn(name = "course_id")
private Course course;
@OneToOne(mappedBy = }
"course", cascade =
CascadeType.ALL) 2. One-to-Many — Instructor → Course
private CourseMaterial
courseMaterial;
@Entity @Entity
} public class Course { public class Instructor {
@Id @GeneratedValue @Id @GeneratedValue
private Long id; private Long id;
private String title; private String name;

@ManyToOne @OneToMany(mappedBy = "instructor", cascade


@JoinColumn(name = "instructor_id") = CascadeType.ALL)
private Instructor instructor; private List<Course> courses;
} }
3. Many-to-Many — Student ↔ Course
@Entity
public class Student {
@Id @GeneratedValue
private Long id;
private String name;

@ManyToMany
@JoinTable(
name = "student_course",
joinColumns = @JoinColumn(name =
"student_id"),
inverseJoinColumns = @JoinColumn(name =
"course_id")
) @Entity
public class Course {
private Set<Course> courses;//or use List
} @Id @GeneratedValue
private Long id;
private String title;

@ManyToMany(mappedBy = "courses")
private Set<Student> students; //or use List

You might also like