Skip to content

Commit d141416

Browse files
author
kmsoln
committed
[Master] Merged jpa
1 parent 91db132 commit d141416

51 files changed

Lines changed: 2713 additions & 0 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

docs/en/jpa/lab-work.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Lab Work: JPA (Java Persistence API)
2+
3+
Welcome to the Spring Boot with JPA Lab Work! In this lab work, you will practice various tasks related to setting up a Spring Boot project with JPA (Java Persistence API).
4+
5+
## Goal of the Lab Work
6+
7+
The goal of this lab work is to provide hands-on experience with key concepts and tasks involved in working with Spring Boot and JPA. By completing the practice tasks, you will gain proficiency in setting up dependencies, defining entity characteristics, managing relationships between entities, and interacting with the database using JpaRepository.
8+
9+
## Practice Tasks <a name="practice-tasks"></a>
10+
11+
1. **Database Dependencies Setup**
12+
- [Database Driver Setup](practice/dependencies/driver.md)
13+
- [Spring Boot and JPA Dependencies](practice/dependencies/jpa.md)
14+
15+
2. **Entity Creation, Characteristics, Relationships and Fetching Strategies**
16+
- [Entity Creation](practice/creation/create-entity.md)
17+
- [Entity Characteristics Definition](practice/creation/characteristics.md)
18+
- [Managing Relationships](practice/creation/relationships.md)
19+
- [Fetching Strategies](practice/creation/fetching.md)
20+
21+
3. **Interacting with Custom Repository**
22+
- [Create Record](practice/interacting/repository/create.md)
23+
- [Read Record](practice/interacting/repository/read.md)
24+
- [Update Record](practice/interacting/repository/update.md)
25+
- [Delete Record](practice/interacting/repository/delete.md)
26+
- [Custom Queries](practice/interacting/repository/query.md)
27+
28+
4. **Interacting with JPA Repository**
29+
- [Create Record](practice/interacting/jpa-repository/create.md)
30+
- [Read Record](practice/interacting/jpa-repository/read.md)
31+
- [Delete Record](practice/interacting/jpa-repository/delete.md)
32+
- [Custom Queries](practice/interacting/jpa-repository/query.md)
33+
34+
35+
## Lab Work Tasks <a name="lab-work-tasks"></a>
36+
37+
1. Set up database dependencies for PostgreSQL, MySQL, Oracle, MongoDB, and Microsoft SQL Server.
38+
39+
2. Implement the next Entity-Relationships Diagram:
40+
![img.png](../../srcs/jpa/task-er-diagram.png)
41+
42+
1. Define entity characteristics for Student, Enrollment, and Course entities based on the Entity Relationships diagram.
43+
44+
2. Create entity classes for Student, Enrollment, and Course according to the defined characteristics.
45+
46+
3. Implement fetching strategies for efficient data retrieval to optimize database interactions.
47+
48+
4. Manage relationships between entities such as Student, Enrollment, and Course by establishing appropriate associations and mappings.
49+
50+
5. Interact with the database by creating methods for CRUD operations using the Repository layer and EntityManager in at least two repositories.
51+
52+
6. Interact with the database by creating methods for CRUD operations using JpaRepository in at least two repositories.
53+
54+
7. Implement custom queries to perform advanced data retrieval and manipulation operations as required by the application's business logic.
55+
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
# Defining Entity Characteristics
2+
3+
## Introduction
4+
5+
This guide focuses on defining entity columns or characteristics in a Spring Boot project using JPA (Java Persistence API).
6+
7+
## Goal
8+
The goal of this guide is to provide clear instructions for defining the characteristics of entities in a Spring Boot project with JPA, utilizing annotations to enforce constraints and behaviors for each attribute.
9+
10+
11+
## Entity Characteristics
12+
13+
### Student Entity Characteristics:
14+
- `id` (UUID): Primary key for the entity.
15+
- `name` (String): Name of the student (must not be blank).
16+
- `email` (String): Email address of the student (must be a valid email address).
17+
- `major` (String): Major or field of study of the student (must not be blank).
18+
- `year` (int): Year of study for the student (must not be null).
19+
- `international` (boolean): Indicates if the student is international (must not be null).
20+
21+
### Define `id` Attribute
22+
23+
Add the `id` attribute to the `Student` class. This attribute will serve as the primary key for the entity.
24+
25+
```java
26+
@Id
27+
@GeneratedValue(strategy = GenerationType.AUTO)
28+
private UUID id;
29+
```
30+
31+
- `@Id`: Indicates that this field is the primary key for the entity.
32+
- `@GeneratedValue`: Specifies the strategy for generating primary key values automatically.
33+
34+
### Define `name` Attribute
35+
36+
Add the `name` attribute to the `Student` class. This attribute represents the name of the student and must not be blank.
37+
38+
```java
39+
@NotBlank
40+
private String name;
41+
```
42+
43+
- `@NotBlank`: Ensures that the value of `name` is not null and contains at least one non-whitespace character.
44+
45+
### Define `email` Attribute
46+
47+
Add the `email` attribute to the `Student` class. This attribute represents the email address of the student and must be a valid email address.
48+
49+
```java
50+
@NotBlank
51+
@Email
52+
private String email;
53+
```
54+
55+
- `@Email`: Validates that the value of `email` is a valid email address.
56+
57+
### Define `major` Attribute
58+
59+
Add the `major` attribute to the `Student` class. This attribute represents the major or field of study of the student and must not be blank.
60+
61+
```java
62+
@NotBlank
63+
private String major;
64+
```
65+
66+
- `@NotBlank`: Ensures that the value of `major` is not null and contains at least one non-whitespace character.
67+
68+
### Define `year` Attribute
69+
70+
Add the `year` attribute to the `Student` class. This attribute represents the year of study for the student and must not be null.
71+
72+
```java
73+
@NotNull
74+
private int year;
75+
```
76+
77+
- `@NotNull`: Ensures that the value of `year` is not null.
78+
79+
### Define `international` Attribute
80+
81+
Add the `international` attribute to the `Student` class. This attribute indicates whether the student is international and must not be null.
82+
83+
```java
84+
@NotNull
85+
private boolean international;
86+
```
87+
88+
- `@NotNull`: Ensures that the value of `international` is not null.
89+
90+
91+
You can repeat the previous steps to create entity characteristics for the `Course` and `Enrollment` entities, ensuring appropriate annotations are applied to each attribute.
92+
93+
### Enrollment Entity Characteristics:
94+
95+
- `id` (UUID): Primary key for the entity.
96+
- `enrollmentDate` (Date): Date of enrollment.
97+
- `grade` (int): Grade achieved in the course.
98+
99+
### Course Entity Characteristics:
100+
101+
- `id` (UUID): Primary key for the entity.
102+
- `name` (String): Name of the course.
103+
- `instructor` (String): Name of the course instructor.
104+
- `schedule` (String): Schedule details of the course.
105+
- `capacity` (int): Maximum capacity of the course.
106+
- `online` (boolean): Indicates if the course is online.
107+
- `startDate` (Date): Start date of the course.
108+
109+
---
110+
111+
# [Next: Managing Relationships](relationships.md)
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Creating Entities
2+
3+
## Introduction
4+
5+
This guide focuses on creating entities in a Spring Boot project using JPA (Java Persistence API). It provides step-by-step instructions for setting up the folder structure and creating entity classes.
6+
7+
## Goal
8+
9+
The goal of this guide is to demonstrate how to create entity classes in a Spring Boot project, following best practices and utilizing JPA for data persistence.
10+
11+
## Folder Structure
12+
13+
To ensure a well-organized project, it's essential to establish a proper folder structure. Follow these guidelines:
14+
15+
```
16+
src/
17+
└── main/
18+
└── java/
19+
└── com/
20+
└── example/
21+
└── demo/
22+
└── entity/
23+
├── Student.java
24+
├── Enrollment.java
25+
└── Course.java
26+
27+
```
28+
29+
In this structure:
30+
- The `src/main/java` directory contains your Java source files.
31+
- The `com/example/demo/entity` directory is where you'll create your entity classes.
32+
33+
## Creating Entity Classes
34+
35+
To create entity classes, follow these steps:
36+
37+
1. Navigate to the `com/example/demo/entity` directory in your project.
38+
2. Create a new Java class file for each entity. You can repeat the following steps for each entity (e.g., Student, Enrollment, Course).
39+
3. Define your entity class with the `@Entity` annotation.
40+
41+
### Example:
42+
43+
```java
44+
package com.example.demo.entity;
45+
46+
import javax.persistence.Entity;
47+
48+
@Entity
49+
public class Student {
50+
51+
}
52+
```
53+
54+
The `@Entity` annotation indicates that this class is a JPA entity.
55+
56+
Repeat the above steps to create `Enrollment` and `Course` entity classes.
57+
58+
---
59+
60+
# [Next: Entity Characteristics Definition](characteristics.md)
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
Understood. Here's the revised document with the code combined with the scenario:
2+
3+
---
4+
5+
# Fetching Strategies in Spring Boot with JPA: Understanding Lazy and Eager Loading
6+
7+
## Introduction
8+
9+
This guide explores the fetching strategies in a Spring Boot project with JPA (Java Persistence API). Specifically, we will delve into the concepts of Lazy and Eager loading and how they are applied in entity relationships.
10+
11+
## Goal
12+
13+
The goal of this guide is to provide a comprehensive understanding of fetching strategies in JPA. By the end of this guide, you will comprehend when to use Lazy and Eager loading and how to implement them in your application.
14+
15+
## Fetching Strategies
16+
17+
### Scenario
18+
19+
In our entity relationships, we have a One-to-Many relationship between the `Student` entity and the `Enrollment` entity. Similarly, there is a One-to-Many relationship between the `Course` entity and the `Enrollment` entity.
20+
21+
#### Student Entity
22+
23+
In the `Student` entity, we have a One-to-Many relationship with the `Enrollment` entity. Each student can have multiple enrollments.
24+
25+
To represent this relationship, we use the `@OneToMany` annotation with `mappedBy = "student"` in the `Student` entity. Here, we have a choice between Lazy and Eager loading strategies for fetching the associated enrollments.
26+
27+
```java
28+
@OneToMany(mappedBy = "student", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
29+
private List<Enrollment> enrollments;
30+
```
31+
32+
We opt for Eager loading (`FetchType.EAGER`) in this scenario. Since we typically expect to access all enrollments of a student together, Eager loading ensures that all enrollments are fetched immediately when loading the student.
33+
34+
#### Course Entity
35+
36+
Similarly, in the `Course` entity, we also have a One-to-Many relationship with the `Enrollment` entity. Each course can have multiple enrollments.
37+
38+
To represent this relationship, we use the `@OneToMany` annotation with `mappedBy = "course"` in the `Course` entity. Here, we have another choice between Lazy and Eager loading strategies for fetching the associated enrollments.
39+
40+
```java
41+
@OneToMany(mappedBy = "course", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
42+
private List<Enrollment> enrollments;
43+
```
44+
45+
We opt for Lazy loading (`FetchType.LAZY`) in this scenario. Since we may not always need to access all enrollments of a course, Lazy loading ensures that enrollments are fetched only when explicitly requested.
46+
47+
---
48+
49+
# [Next: Create Repository](../interacting/repository.md)
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Defining Entity Relationships
2+
3+
## Introduction
4+
5+
This guide delves into understanding and implementing relationships between entities in a Spring Boot project using JPA (Java Persistence API). Specifically, we will explore the relationship between the `Enrollment` entity and its associated entities `Student` and `Course`.
6+
7+
## Goal
8+
9+
The goal of this guide is to provide clear instructions for defining and understanding entity relationships in a Spring Boot project with JPA. By the end of this guide, you will comprehend how to establish associations between entities and implement them in your application.
10+
11+
## Entity Associations
12+
13+
![img.png](../../../../srcs/jpa/er-diagram.png)
14+
### Enrollment Entity Characteristics:
15+
16+
- `id` (UUID): Primary key for the entity.
17+
- `student` (Student): Student associated with the enrollment.
18+
- `course` (Course): Course associated with the enrollment.
19+
- `enrollmentDate` (Date): Date of enrollment.
20+
- `grade` (int): Grade achieved in the course.
21+
22+
#### Student Relationship
23+
24+
The relationship between the `Enrollment` entity and the `Student` entity represents a scenario where a student enrolls in a course. This is a Many-to-One relationship, where many enrollments can be associated with one student. To establish this relationship, annotate the `student` attribute in the `Enrollment` entity with `@ManyToOne`.
25+
26+
```java
27+
@ManyToOne
28+
@JoinColumn(name = "student_id")
29+
private Student student;
30+
```
31+
32+
#### Course Relationship
33+
34+
Similarly, the relationship between the `Enrollment` entity and the `Course` entity signifies a scenario where a course is enrolled by multiple students. This is also a Many-to-One relationship, where many enrollments can be associated with one course. Annotate the `course` attribute in the `Enrollment` entity with `@ManyToOne`.
35+
36+
```java
37+
@ManyToOne
38+
@JoinColumn(name = "course_id")
39+
private Course course;
40+
```
41+
42+
---
43+
44+
# [Next: Fetching Strategies](fetching.md)

0 commit comments

Comments
 (0)