Home > other >  Finding the number of related tables in a ManyToMany relationship in JPA-Spring Boot
Finding the number of related tables in a ManyToMany relationship in JPA-Spring Boot

Time:11-06

I have @ManyToMany related entity structures named Student and Course.I would like a student to be able to register for a maximum of 3 courses.At the same time, a course must have a maximum of 10 student. How can i do that? (Also I am using mySql database and hibernate)

Here is my Student class;

@Entity
@NoArgsConstructor
@Getter
@Setter
public class Student extends BaseEntity {
    private String name;
    private String surname;
    @Column(name = "student_number",unique = true)
    private String number; //student number

    @JsonIgnore
    @ManyToMany(cascade = CascadeType.MERGE, fetch = FetchType.EAGER)
    @JoinTable(name = "students_courses", joinColumns = @JoinColumn(name = "student_id"),
            inverseJoinColumns = @JoinColumn(name = "course_id"))
    private List<Course> courseList = new ArrayList<>();

}

Course Class;

@Entity
@Getter
@Setter
public class Course extends BaseEntity{

    @Column(name = "course_name",unique = true)
    private String courseName;

    @JsonIgnore
    @ManyToMany(mappedBy = "courseList",cascade = CascadeType.ALL ,fetch = FetchType.EAGER)
    private List<Student> studentList = new ArrayList<>();

}

Repositories;

@Repository
public interface StudentRepository extends JpaRepository<Student,Long> {

}
@Repository
public interface CourseRepository extends JpaRepository<Course,Long> {

}

CodePudding user response:

Given an object studentRepository implementing StudentRepository you can get the student by id Student student = studentRepository.findById(studentId) (assuming there is an id in your BaseEntity which I think there is).

Then you can test if student.courseList.size()<=MAX_NUMBER_OF_COURSES, for example, before saving studentRepository.save(student) or something else.

The same logic can be applied to courses. Not sure if there is anything more idiomatic to JPA and/or in Spring Boot.

CodePudding user response:

Thanks for your reply.I did something like this and it works for Student.

/*
 * This Dto class is used to assign courses to a student
 *
 * */
 @Getter
 @Setter
 public class StudentCourseDto {
 private Long student_id;
 private List<Long> course_id_List;
 }

I wrote this code in StudentServiceImp;

@Transactional(rollbackFor = Exception.class)
@Override
public void addCourse(StudentCourseDto studentCourseDto) {

    Student student = mapper.map(
            this.studentRepository.findById(studentCourseDto.getStudent_id()).get(), Student.class);

    for (Long id : studentCourseDto.getCourse_id_List()) {

        //A student cannot be registered in more than 5 courses.
        if (studentCourseDto.getCourse_id_List().size() < 5) {
            Course course = this.courseRepository.findById(id).orElse(null);
            student.getCourseList().add(course);
        }
    }
}

But I can't do it for course class. Because I can add data from one side in ManyToMany relation. Do you have a solution for this?

  • Related