I have an entity Student and an entity Course. One student can be associated to 0 or more Courses. Viceversa, one Course can have associated 0 or more Students.
Student entity:
@Data
@Entity(name = "student")
public class Student {
@Id
private Integer id;
private String name;
@ManyToMany(fetch = EAGER)
@JoinTable(name = "student_course",
joinColumns = @JoinColumn(
name = "studentId",
referencedColumnName = "id",
insertable = false,
updatable = false
),
inverseJoinColumns = @JoinColumn(
name = "courseId",
referencedColumnName = "id",
insertable = false,
updatable = false)
)
private Collection<Course> courses;
}
Course entity:
@Data
@Entity(name = "course")
public class Course {
@Id
private Integer id;
private String name;
@ManyToMany(mappedBy = "courses")
private Collection<Student> students;
}
and the inverse association in the entity Course.
Both of those 2 @ManyToMany associations should be readonly. My problem is that when I try to save a Student Hibernate tries to update also the associated collection.
Here's what hibernate logs on a student update:
Hibernate:
/* delete collection model.student.courses */
delete from `student_course`
where `studentId`=?
Hibernate:
/* insert collection row */
insert into `student_course` (`studentId`, `courseId`)
values (?, ?)
As you can see hibernate is trying to update also the table that stores the associations between the two entities. Those are the queries that I want to avoid.