I try to post events and do this in general. I mean, create one abstract DAO base class with a common type and fire the event from its method. This should work for all descendants. This works if I define the exact type, but not if I use generics. What I mean:
AbstractDAO (with generics - does not fire an event):
public abstract class AbstractDAO<T extends Persistable> implements Serializable { @Inject @PostSaveEvent Event<T> postSaveEvent; public T saveOrUpdate(T object) throws DatabaseException { T obj = em.merge(object); postSaveEvent.fire(obj); } }
AbstractDAO (no generics, just a cool class - the event fires):
public abstract class AbstractDAO<T extends Persistable> implements Serializable { @Inject @PostSaveEvent Event<Polis> postSaveEvent; public T saveOrUpdate(T object) throws DatabaseException { T obj = em.merge(object); postSaveEvent.fire((Polis)obj); } }
The PolisDAO class, which extends AbstractDAO and defines a generic type:
@Stateless @Named @PolisType public class PolisDAO extends AbstractDAO<Polis> {
My observer class:
@Stateless @Named public class ProlongationService { public void attachProlongationToPolisOnSave(@Observes @PostSaveEvent Polis polis) throws DatabaseException {
This is very strange for me, since the "fire ()" method for a CDI event should determine the type of event at runtime, and not at compile time or deployment ... When I debug, I see that
postSaveEvent.fire(obj);
from the first sample works exactly with the Polis object. However, not a single event was fired ...
Update. I tried the base general class but no luck:
@Inject @PostSaveEvent Event<Persistable> postSaveEvent;
Thanks.
bitec source share