openjpa-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From David Beer <david.m.b...@googlemail.com>
Subject Problem With Entity Listeners
Date Tue, 27 Sep 2011 14:41:40 GMT
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

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message