-->
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: EntityListeners with ManyToMany collections
PostPosted: Fri Feb 26, 2010 7:29 pm 
Newbie

Joined: Mon Jun 23, 2008 12:16 pm
Posts: 1
I am using the Hibernate JPA and have an entity which contains a ManyToMany collection. I would like to be notified when changes to the collection have been changed. I tried using an EntityListener, however when an item was added to the collection, the listener's @PreUpdate/@PostUpdate methods were not called. Below is some code of the entity setup I am using.

Any suggestions on how to receive notifications on changes to the collections contained in an entity? Thanks.

Code:
@Entity
@Inheritance(strategy=InheritanceType.JOINED)
public class Group{
   private String name;
   
   @ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
   private Set<Person> members = new HashSet<Person>();   
   
   public void addMember(Person person){
      members.add(person);
   }
   
   public Set<Person> getMembers(){
      return members;
   }
   
   public void setMembers(Set<Person> members){
      this.members = members;
   }
   //...
}

@Entity
@EntityListeners({TeamEntityListener.class})
public class Team extends Group{
   private Person coach;   
   //...
}

@Entity
public class Person{
   private String name;   
   //...
}

public class TeamEntityListener{
   @PostUpdate
   public void postUpdate(Team team){
      System.out.println("The team " + team.getName() + " has been updated");
      //update code
   }
}


Person john = new Person("John Doe");
entityManager.persist(john);

Team team = new Team();
team.setName("TeamName");
entityManager.persist(team);
//..
team.addMember(john);
entityManager.merge(entity);  //No update notification received for the team
//..
HashSet<Person> members = new HashSet<Person>(team.getMembers());
members.add(new Person("Jane Doe"));
team.setMembers(members);
entityManager.merge(entity);  //No update notification received for the team


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.