Skip to content

Commit 5aa99c8

Browse files
author
kmsoln
committed
Merge branch 'jpa/docs'
# Conflicts: # docs/en/jpa/practice/creation/characteristics.md # docs/en/jpa/practice/creation/create-entity.md # docs/en/jpa/practice/creation/fetching.md # docs/en/jpa/practice/creation/relationships.md # docs/en/jpa/practice/dependencies/driver.md # docs/en/jpa/practice/dependencies/jpa.md # docs/en/jpa/practice/interacting/jpa-repository.md # docs/en/jpa/practice/interacting/jpa-repository/create.md # docs/en/jpa/practice/interacting/jpa-repository/delete.md # docs/en/jpa/practice/interacting/jpa-repository/query.md # docs/en/jpa/practice/interacting/jpa-repository/read.md # docs/en/jpa/practice/interacting/repository.md # docs/en/jpa/practice/interacting/repository/create.md # docs/en/jpa/practice/interacting/repository/delete.md # docs/en/jpa/practice/interacting/repository/query.md # docs/en/jpa/practice/interacting/repository/read.md # docs/en/jpa/practice/interacting/repository/update.md # docs/ru/jpa/practice/creation/characteristics.md # docs/ru/jpa/practice/creation/create-entity.md # docs/ru/jpa/practice/creation/fetching.md # docs/ru/jpa/practice/creation/relationships.md # docs/ru/jpa/practice/dependencies/driver.md # docs/ru/jpa/practice/dependencies/jpa.md # docs/ru/jpa/practice/interacting/jpa-repository.md # docs/ru/jpa/practice/interacting/jpa-repository/create.md # docs/ru/jpa/practice/interacting/jpa-repository/delete.md # docs/ru/jpa/practice/interacting/jpa-repository/query.md # docs/ru/jpa/practice/interacting/jpa-repository/read.md # docs/ru/jpa/practice/interacting/repository.md # docs/ru/jpa/practice/interacting/repository/create.md # docs/ru/jpa/practice/interacting/repository/delete.md # docs/ru/jpa/practice/interacting/repository/query.md # docs/ru/jpa/practice/interacting/repository/read.md # docs/ru/jpa/practice/interacting/repository/update.md
2 parents 27387ae + c962e99 commit 5aa99c8

34 files changed

Lines changed: 1838 additions & 0 deletions
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)
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
# JPA: Setup Dependencies
2+
3+
## Introduction
4+
5+
This guide focuses on setting up dependencies for a Spring Boot project with JPA (Java Persistence API). It covers the necessary steps to include database drivers for various databases such as PostgreSQL, MySQL, Oracle, MongoDB, and Microsoft SQL Server. By following this guide, you will configure all the required dependencies to establish connections with these databases in your Spring Boot application.
6+
7+
## Goal
8+
The goal of this guide is to provide clear instructions for setting up the necessary dependencies for Spring Boot and JPA, specifically focusing on database drivers for different databases.
9+
10+
## Setup Dependencies
11+
12+
### Database Drivers
13+
14+
To connect to different databases in your Spring Boot application, include the appropriate database drivers.
15+
16+
#### PostgreSQL
17+
18+
**For Gradle:**
19+
```groovy
20+
implementation "org.postgresql:postgresql"
21+
```
22+
23+
**For Maven:**
24+
```xml
25+
<dependency>
26+
<groupId>org.postgresql</groupId>
27+
<artifactId>postgresql</artifactId>
28+
<version>INSERT_VERSION_HERE</version>
29+
</dependency>
30+
```
31+
32+
#### MySQL
33+
34+
**For Gradle:**
35+
```groovy
36+
implementation "mysql:mysql-connector-java"
37+
```
38+
39+
**For Maven:**
40+
```xml
41+
<dependency>
42+
<groupId>mysql</groupId>
43+
<artifactId>mysql-connector-java</artifactId>
44+
<version>INSERT_VERSION_HERE</version>
45+
</dependency>
46+
```
47+
48+
#### Oracle Database Driver
49+
50+
**For Gradle:**
51+
```groovy
52+
implementation 'com.oracle.database.jdbc:ojdbc8'
53+
```
54+
55+
**For Maven:**
56+
```xml
57+
<dependency>
58+
<groupId>com.oracle.database.jdbc</groupId>
59+
<artifactId>ojdbc8</artifactId>
60+
</dependency>
61+
```
62+
63+
#### MongoDB Driver (MongoDB Java Driver)
64+
65+
**For Gradle:**
66+
```groovy
67+
implementation 'org.mongodb:mongodb-driver-sync:4.4.3'
68+
```
69+
70+
**For Maven:**
71+
```xml
72+
<dependency>
73+
<groupId>org.mongodb</groupId>
74+
<artifactId>mongodb-driver-sync</artifactId>
75+
<version>4.4.3</version>
76+
</dependency>
77+
```
78+
79+
#### Microsoft SQL Server JDBC Driver
80+
81+
**For Gradle:**
82+
```groovy
83+
implementation 'com.microsoft.sqlserver:mssql-jdbc:9.4.0.jre11'
84+
```
85+
86+
**For Maven:**
87+
```xml
88+
<dependency>
89+
<groupId>com.microsoft.sqlserver</groupId>
90+
<artifactId>mssql-jdbc</artifactId>
91+
<version>9.4.0.jre11</version>
92+
</dependency>
93+
```
94+
95+
These dependencies facilitate database connectivity.
96+
97+
---
98+
99+
# [Next: Spring Boot and JPA Dependencies](jpa.md)
100+

0 commit comments

Comments
 (0)