-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: EntityListner on @onetoMany relationship
PostPosted: Fri Mar 28, 2014 3:57 am 
Newbie

Joined: Fri Mar 28, 2014 3:51 am
Posts: 1
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.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.