Good morning, David. The @PostPersist callback isn't guaranteed to fire at the moment em.persist(entity) or em.merge(newEntity) is called. What is guaranteed is that it will fire when the transaction completes (I think calling em.flush() will also trigger any pending PostPersist callback invocations as well.)
On the otherhand, @PrePersist is guaranteed to fire when persist() or merge(newEntity) is called. For your situation, you may be better off using @PrePersist for handling update notifications to your UI. Now, @PrePersist and @PostPersist fire only when new rows are to be added to the database. For modifications of existing rows (such as entities acquired by em.find() or queries, merging an edited detached entity, or further modifications to a still-managed new entity whose row has been committed to the database, it is the @PreUpdate and @PostUpdate callbacks are fired. On Tue, Sep 27, 2011 at 9:41 AM, David Beer <david.m.b...@googlemail.com>wrote: > Hi All > > I am try to listen for events when an Entity is persisted, updated and > removed. This is simply so that I can update the model which handles the > UI. > I have successfully added @EntityListeners(EntryListener.class) anotation > to > the entity class. My EntryListener class looks like the following. > > public class EntryListener implements EntityListener{ > > private PropertyChangeSupport pss = new PropertyChangeSupport(this); > private EntityNotification en = > Lookup.getDefault().lookup(EntityNotification.class); > > /** > * Blank no arg constructor > */ > public EntryListener() { > } > > @PostPersist > @Override > public void postPersist(Object obj) { > en.postPersist(obj); > } > > @Override > public void prePersist(Object obj) { > Logger.getLogger(EntryListener.class.getName()).log(Level.INFO, > "prePersist fired"); > } > > @Override > public void preUpdate(Object obj) { > Logger.getLogger(EntryListener.class.getName()).log(Level.INFO, > "preUpdate fired"); > } > > @Override > public void postUpdate(Object obj) { > Logger.getLogger(EntryListener.class.getName()).log(Level.INFO, > "postUpdate fired"); > en.postUpdate(obj); > } > > @Override > public void postRemove(Object Obj) { > Logger.getLogger(EntryListener.class.getName()).log(Level.INFO, > "postRemove fired"); > } > > @Override > public void preRemove(Object obj) { > Logger.getLogger(EntryListener.class.getName()).log(Level.INFO, > "preRemove fired"); > } > > @Override > public void addPropertyChangeListener(PropertyChangeListener pcl) { > pss.addPropertyChangeListener(pcl); > } > > @Override > public void removePropertyChangeListener(PropertyChangeListener pcl) { > pss.removePropertyChangeListener(pcl); > } > > } > > The problem is that I seem to be able to catch the event for @postPersist > when a new item is added to the database. I am not closing the > EntityManager > each time as a lot of transactions can occur. But am getting the data > straight from the db when updated and then passed to the UI. This works > well > for when I add an entry with em.persist. But If I update an item in the UI > and try to either persist the updated item or merge the item no event is > being thrown. > > Any advice is much appreciated. > > Thanks > > David >