
1. 理解多对多关系及其挑战
在基于jpa和spring boot的领域模型中,多对多(@manytomany)关系是常见的关联类型,例如学生与课程之间的关系:一个学生可以注册多门课程,一门课程也可以被多个学生注册。jpa通过关联表(join table)来管理这种关系。
然而,@ManyToMany注解本身并不能直接强制执行更复杂的业务规则,例如:
- 一个学生最多只能注册3门课程。
- 一门课程最多只能有10名学生。
这些业务约束需要通过应用层的逻辑来实现。
2. 实体模型定义
为了演示,我们首先定义Student和Course两个实体。它们通过students_courses关联表建立多对多关系。
免费 盛世企业网站管理系统(SnSee)系统完全免费使用,无任何功能模块使用限制,在使用过程中如遇到相关问题可以去官方论坛参与讨论。开源 系统Web代码完全开源,在您使用过程中可以根据自已实际情况加以调整或修改,完全可以满足您的需求。强大且灵活 独创的多语言功能,可以直接在后台自由设定语言版本,其语言版本不限数量,可根据自已需要进行任意设置;系统各模块可在后台自由设置及开启;强大且适用的后台管理支
Student 实体:
import com.fasterxml.jackson.annotation.JsonIgnore;
import jakarta.persistence.*;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import java.util.ArrayList;
import java.util.List;
@Entity
@NoArgsConstructor
@Getter
@Setter
public class Student extends BaseEntity { // 假设 BaseEntity 包含 ID 字段
private String name;
private String surname;
@Column(name = "student_number", unique = true)
private String number; // 学号
@JsonIgnore
@ManyToMany(cascade = {CascadeType.MERGE, CascadeType.PERSIST}, fetch = FetchType.EAGER)
@JoinTable(name = "students_courses",
joinColumns = @JoinColumn(name = "student_id"),
inverseJoinColumns = @JoinColumn(name = "course_id"))
private List courseList = new ArrayList<>();
// 辅助方法,用于维护双向关系
public void addCourse(Course course) {
if (!this.courseList.contains(course)) {
this.courseList.add(course);
// 确保 Course 端的学生列表也更新
if (!course.getStudentList().contains(this)) {
course.getStudentList().add(this);
}
}
}
public void removeCourse(Course course) {
if (this.courseList.contains(course)) {
this.courseList.remove(course);
// 确保 Course 端的学生列表也更新
course.getStudentList().remove(this);
}
}
} Course 实体:
import com.fasterxml.jackson.annotation.JsonIgnore;
import jakarta.persistence.*;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import java.util.ArrayList;
import java.util.List;
@Entity
@NoArgsConstructor
@Getter
@Setter
public class Course extends BaseEntity { // 假设 BaseEntity 包含 ID 字段
@Column(name = "course_name", unique = true)
private String courseName;









