I am new in Hibernate. I am developing a backend service with Java and Hibernate.
I have a Student
entity, it has a many-to-many relationship with Group
entity. That's in database, I have a student
table and a group
table:
@Entity
@Table(name = "student")
public class Student {
private int student_id; //primary key
@ManyToMany(mappedBy = "students")
private Set<Group> groups = new HashSet<Group>();
@ManyToMany(cascade = {CascadeType.ALL})
@JoinTable(name="student_group",
joinColumns={@JoinColumn(name="student_id")},
inverseJoinColumns={@JoinColumn(name="group_id")})
public Set<Group> getGroups() {
return groups;
}
public void setGroups(Set<Group> groups) {
this.groups = groups;
}
//Setter & Getter for student_id
...
}
As you see in above hibernate annotation, there is a join table student_group
in database. What I want to achieve is that, when a client's request is arriving to my backend service (with a student id), I would like to know whether the student_group
join table in database has updated for this student, e.g. whether a new row (new group) is inserted (assigned to this student) or whether an existing row has been updated value. If there is such update/change, I will inform client.
With hibernate, how can I implement this kind of listener to listen to the change/update in database table?