Spring Data JPA
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:
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
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;
@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