'Modeling Entities in SpringBoot while having the database Diagram using Hibernate

So I have the database diagram of an application, and I'm not really sure of these two things:

First, I have the tables Students and Courses and a relationship between them StudentCourses. Am I supposed to have 3 entities here, or have Students have a @ManyToOne field of a List and Course have a @ManyToOne field of List? If it is the first option, how do I store each of them - do I store a StudentCourse in both Students and Courses?

Secondly, if I have the primary key of StudentCourse to be the tuple of foreign keys of Student and Course, and I create a composite key like this:

package com.example.models;

import java.io.Serializable;

public class CompositeKey implements Serializable {
    private Integer firstKey;
    private Integer secondKey;

    public CompositeKey(){

    }

    public CompositeKey(Integer first_key, Integer second_key){
        this.firstKey = first_key;
        this.secondKey = second_key;
    }

    public Integer getFirstKey(){
        return firstKey;
    }

    public Integer getSecondKey(){
        return secondKey;
    }

}

am I able to do the following:

public class StudentCourse{
    @Id
    @ManyToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "student_id")
    private Student student;

    @Id
    @ManyToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "course_id")
    private Course course;

Thank you



Solution 1:[1]

You don't need to create extra table for relationship for course and student, hibernate automatically creates that table. If you need to store extra column in that relation entity then you can alsa create that entity, just careful about the naming convention so that hibernate sees as relation entity of these two.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 a_meydanal