I am doing some audit logging through @entitylistners of java persistence API. I have a scenario of one to many entity relationship where one student can have multiple subjects.
Student Entity
Code:
@Entity
@Table(name = "student")
@EntityListeners(AuditInterceptor.class)
public class Student
{
@id
@column("student_id")
private Integer studentId;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "student", cascade = CascadeType.ALL,orphanRemoval=true)
private Set<Subjects> subjects= new HashSet<Subjects>(0);
// getter and setter
}
SUBJECT Entity
Code:
@Entity
@Table(name = "subject")
public class Subject
{
@id
@column("subject_id")
private Integer subjectId;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "student_id")
private Student student;
// getter and setter
}
Audit Interceptor Class
Code:
public class AuditInterceptor
{
@PreUpdate
public void preUpdate(Object entity)
{
//anycode
}
}
on UI,one student can select multiple subjects. So whenever student entity gets updated, my call to @preUpdate method comes in. here are possible cases of auditing.
When student entity gets change (like student name)--control should go to @preUpdate
When student and subject both gets change (add/remove subject list to student AND update student name)--control should go to @preUpdate
When only subject get change (only add/remove subject list)
My 3rd case is failing Question: Whenever student's subject gets changed then my interceptor is not calling. On dao layer I am just using JPA repository SAVE method for updating he entity. I need whenever my child entity gets changed then my parent entity reference should also needs to change so that my interceptor can understand that Student entity has been changed.