|
| 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) |
0 commit comments