3_Spring_Data_JPA
3_Spring_Data_JPA
1
Chapter 3: SPRING DATA JPA
OUTLINE
1. Getting started with Spring Data JPA
2. Create a New Spring Boot Project
2
Getting started with Spring Data JPA
§ Spring Data JPA is a powerful framework that allows
users to easily interact with their database while
minimizing boilerplate code.
§ In this tutorial, we're going to look at how to use Spring
Data JPA to insert into and query data from a database.
§ We'll create a simple Spring Boot application
using IntelliJ IDEA Ultimate to take advantage of its
Spring feature support.
3
Creating a New Spring Boot Project
4
Creating a New Spring Boot Project (cont.)
5
Creating a New Spring Boot Project (cont.)
6
Creating an Employee Entity
7
Creating an Employee Repository Interface
Create a Spring Data repository for CRUD operations.
In order for our interface to be a repository, we'll need it to
extend the CrudRepository<T, ID> or JpaRespository<T,
ID> interface with the generic parameters being our entity
class and entity's id type.
So for our application, our repository interface definition
would be:
8
Creating an Employee Repository Interface (cont.)
9
Creating an Employee Repository Interface (cont.)
10
Configuring Database
11
Configuring Database (cont.)
12
Configuring Database (cont.)
13
Configuring Database (cont.)
14
Configuring Database (cont.)
Disable Default Schema Generation and Use SQL Scripts – Create schema and
Import data (resources/schema.sql, resources/data.sql)
Disable Default Schema Generation and Use SQL Scripts – Create schema and
Import data (not default folder)
15
Writing from your Application to the DB
16
Writing from your Application to the DB (cont.)
17
Writing from your Application to the DB (cont.)
18
Creating a Data Source
19
Creating a Data Source (cont.)
20
Spring Data JPA - Create
21
Spring Data JPA - Read
22
Spring Data JPA – Read – Unit Test
23
Spring Data JPA – Read – Unit Test (cont.)
24
Spring Data JPA - Update
25
Spring Data JPA - Delete
26
Spring Data JPA - Search
27
JPA
28
OneToMany - ManyToOne
29
OneToMany – ManyToOne: Generate table
30
OneToMany – ManyToOne: Create data
31
OneToMany – ManyToOne: Get data
32
OneToMany – ManyToOne: Query Result
33
Relationship @OneToOne
34
Relationship @OneToOne (cont.)
35
Relationship @OneToOne (cont.)
36
Relationship @OneToOne (cont.)
37
Relationship @OneToMany
38
Relationship @OneToMany (cont.)
39
Relationship @OneToMany (cont.)
40
Relationship @OneToMany (cont.)
41
Relationship @ManyToOne (cont.)
42
Relationship @ManyToOne (cont.)
43
Relationship @ManyToMany (cont.)
44
Relationship @ManyToMany (cont.)
45
FetchType - LAZY
46
FetchType - EAGER
47
Uni & Bidirectional Relationship - mappedBy
48
@GeneratedValue - GenerationType.INDENTITY
49
@GeneratedValue - GenerationType.AUTO
50
@GeneratedValue - GenerationType.SEQUENCE
51
@GeneratedValue - GenerationType.TABLE
52
Understanding Repository and
their methods
53
JpaRepository
54
JpaRepository (cont.)
55
JpaRepository (cont.)
56
JpaRepository (cont.)
57
JpaRepository (cont.)
https://docs.spring.io/spring-data/jpa/reference/jpa/query-methods.html
58
JpaRepository (cont.)
https://docs.spring.io/spring-data/jpa/reference/jpa/query-methods.html
59
JpaRepository (cont.)
https://docs.spring.io/spring-data/jpa/reference/jpa/query-methods.html
60
@Query - JPQL
61
@Query
https://docs.spring.io/spring-data/jpa/reference/jpa/query-methods.html#jpa.query-methods.at-query
62
@Query – Native Query
63
@Query – Positional Parameters
64
@Query – Named Parameters
65
Q&A
66