Session 03: Spring Data JPA (cont)
Joining Tables
Objectives
• Entity Relationships
• Common Relationship Types:
- One-to-One
- Many-to-One
- One-to-Many
- Many-to-Many
• CascadeType
• FetchType
R2S Academy - Internal Use 2
Entity Relationships (1)
What is Entity Relationship?
• Database Diagram
R2S Academy - Internal Use 3
Entity Relationships (2)
What is Entity Relationship?
• An entity relationship refers to the association between two or more entity
classes that reflect the relationships between tables in a relational database
R2S Academy - Internal Use 4
Entity Relationships (3)
What is Entity Relationship?
One-to-One
This indicates that one student can have one pro ile,
and each student is assigned to only one pro ile.
R2S Academy - Internal Use 5
f
f
Entity Relationships (4)
What is Entity Relationship?
This indicates that one classroom can have multiple students,
but each student is assigned to only one classroom.
One-to-Many
R2S Academy - Internal Use 6
Entity Relationships (5)
What is Entity Relationship?
Multiple instances of entity Student are associated
with a single instance of entity ClassRoom.
One-to-Many
R2S Academy - Internal Use 7
Entity Relationships (6)
What is Entity Relationship?
y
an
-M
to
y-
an
M
Student entities can be associated with multiple Course
entities, and vice versa.
R2S Academy - Internal Use 8
One-to-One (1)
Introduction
• @OneToOne annotation is used to create a one-to-one association between
two entities.
One-to-One
R2S Academy - Internal Use 9
One-to-One (2)
Example
@Entity
public class Profile {
@Id One-to-One
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Entity
private String email; public class Student {
private String phoneNumber; @Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
// Standard getters and setters private Long id;
}
private String name;
@OneToOne
@JoinColumn(name = "profile_id", referencedColumnName = "id")
private Profile profile;
// Standard getters and setters Primary key of
} Profile
R2S Academy - Internal Use 10
One-to-Many & Many-to-One (1)
Introduction
• @OneToMany annotation is used to create a one-to-one association between
two entities.
One-to-Many
Many-to-One
R2S Academy - Internal Use 11
One-to-Many & Many-to-One(2)
Example
@Entity
public class ClassRoom {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id; @Entity
public class Student {
private String name; @Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
// OneToMany relationship with Student private Long id;
@OneToMany(mappedBy = "classRoom") private String name;
private Set<Student> students = new HashSet<>();
@OneToOne
// Standard getters and setters @JoinColumn(name = "profile_id", referencedColumnName = "id")
} private Profile profile;
@ManyToOne
@JoinColumn(name = "class_id", referencedColumnName = "id")
private ClassRoom classRoom;
// Standard getters and setters
}
R2S Academy - Internal Use 12
Many-to-Many (1)
Introduction
• @ManyToMany annotation is used to create a one-to-one association
between two entities.
y
an
-M
to
y-
an
M
R2S Academy - Internal Use 13
Many-to-Many (2)
Example
@Entity @Entity
public class Course { public class Student {
@Id @Id
@GeneratedValue(strategy = GenerationType.IDENTITY) @GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id; private Long id;
private String name; private String name;
private Integer duration; @OneToOne
@JoinColumn(name = "profile_id", referencedColumnName = "id")
// Standard getters and setters private Profile profile;
}
@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private Set<Course> courses = new HashSet<>();
// Standard getters and setters
}
The join table (student_course) that maps the
associations between students and courses.
R2S Academy - Internal Use 14
mappedBy Attribute
When to use mappedBy?
• The mappedBy attribute is used to define the inverse side of a bidirectional relationship
• Which means it doesn’t contain the foreign key column in the database
mappedBy mappedBy
foreign key
column
R2S Academy - Internal Use 15
CascadeType (1)
Introduction
• This option specifies which operations (CUD) should be cascaded from the
parent entity to the associated child entity
@Entity Parent
public class Student {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id; Specifies which
operations
private String name;
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "profile_id", referencedColumnName = "id")
private Profile profile;
Child
// Standard getters and setters
}
R2S Academy - Internal Use 16
CascadeType (2)
Other types include
• PERSIST: Only persist (save) operations are cascaded.
• MERGE: Only merge (update) operations are cascaded.
• REMOVE: Only remove (delete) operations are cascaded.
@Entity
public class Student {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
@OneToOne(cascade = CascadeType.PERSIST)
@JoinColumn(name = "profile_id", referencedColumnName = "id")
private Profile profile;
// Standard getters and setters
}
// Example of multiple cascade types
@OneToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE})
R2S Academy - Internal Use 17
CascadeType (3)
Default Type
• PERSIST: Only persist (save) operations are cascaded.
• MERGE: Only merge (update) operations are cascaded.
• REMOVE: Only remove (delete) operations are cascaded.
@Entity
public class Student {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
When you do not specify a @OneToOne
CascadeType, the default is that no @JoinColumn(name = "profile_id", referencedColumnName = "id")
cascade operations are applied. private Profile profile;
// Standard getters and setters
}
R2S Academy - Internal Use 18
FetchType (1)
Introduction
• This option determines how the related entities should be loaded in
relation to the parent entity
@Entity
public class Student {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name = "profile_id", referencedColumnName = "id")
private Profile profile;
// Standard getters and setters
}
R2S Academy - Internal Use 19
FetchType (2)
Two Main Types
• LAZY: Meaning they are not loaded until they are explicitly accessed in the code. This is
useful for improving performance by avoiding unnecessary data loading.
• EAGER: Meaning they are loaded immediately with the parent entity. This can be useful
when you always need the associated entities and want to avoid multiple queries to the
database
• Example
@Entity @Entity
public class Student { @Table(name = "employee")
@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER) public class Employee {
@JoinColumn(name = "profile_id", referencedColumnName = "id") @OneToMany(mappedBy = "employee", fetch = FetchType.LAZY)
private Profile profile; private Set<Order> orders = new HashSet<>();
} }
R2S Academy - Internal Use 20
FetchType (3)
Default Type
• Here's a table summarizing the default fetch types Without specifying
@Entity
Relationship Default FetchType @Table(name = "employee")
public class Employee {
@OneToMany(mappedBy = "employee")
OneToMany LAZY private Set<Order> orders = new HashSet<>();
}
ManyToMany LAZY
OneToOne EAGER @Entity
@Table(name = "employee")
public class Employee {
@OneToMany(mappedBy = "employee", fetch = FetchType.LAZY)
ManyToOne EAGER private Set<Order> orders = new HashSet<>();
}
R2S Academy - Internal Use 21
Keeping up those inspiration and the enthusiasm in the learning path.
Let confidence to bring it into your career path for getting gain the success as
your expectation.
Thank you
Contact
- Name: R2S Academy
- Email: [email protected]
Questions and Answers
- Hotline/Zalo: 0919 365 363
- Website: https://r2s.edu.vn
- Fanpage: https://www.facebook.com/r2s.tuyendung
R2S Academy - Internal Use 22