So I've been recently creating a ManyToMany Table with custom attributes in Spring Boot. There are the usual 2 foreign keys in it and extra simple attributes as two Longs. The middle table is getting filled as it should, but how do I also manually/programmically set these values.
My ManyToMany Table looks as following:
@Getter
@Setter
@NoArgsConstructor
@Entity(name = "StudentCourseEntity")
@Table(name = "student_courses")
public class StudentCourses implements Serializable {
@EmbeddedId
private StudentCourseId studentCourseId;
@Column
private Long level;
@Column
private Long position;
}
The @EmbeddedId -> StudentCourseId class just has both foreign keys in it. Is it even possible with this ManyToMany variation of just "overwriting" the auto generated ManyToMany table as I did in the example above?
My Student and Course entities don't have a reference on this StudentCourses class, they still have the @ManyToMany implementation to each other.
I'm looking forward for useful answers, thanks guys!
CodePudding user response:
You have to create an entity of Join Table and add extra fields according to your need. Refer here for complete information.