bmojanoski
bmojanoski

Reputation: 35

@ManyToMany java spring boot

This is STUDENTS ENTITY

@Entity
@Table(name = "STUDENTS")
public class Student extends BaseEntity {

  @Column(name = "student_index")
  public String index;
  public String firstName;
  public String lastName;
  @ManyToMany
  public List<Course> courses;
}

This is COURSES ENTITY

@Entity
@Table(name = "COURSES")
public class Course extends BaseEntity {

  public String name;
  @ManyToMany
  public List<Student> students;

}

Which tables will be generated in DataBase?

Upvotes: 0

Views: 74

Answers (1)

Vishnu Atrai
Vishnu Atrai

Reputation: 2368

There must be tables STUDENTS, COURSES, STUDENTS_COURSES, COURSES_STUDENTS

The @ManyToMany will be confused and will create two tables STUDENTS_COURSES, COURSES_STUDENTS

To avoid such situation need to provide option mappedBy ie. @ManyToMany(mappedBy = "STUDENTS") it will create only one table STUDENTS_COURSES

Upvotes: 3

Related Questions